Tutorials/Safe home

Since there are various sources of danger to your buildings and yourself, here are the most common techniques to counter them.

Lava and water
When you extend your base, be sure that nothing fragile like torches or redstone circuits are on the floor in the area. If you strike a water source, it can wash such things away. If you are unlucky, you yourself can also be swept away (maybe into an abyss) or drown.

Lava flows slower, but is much more hazardous, as it sets you and your wooden items on fire.

Sand and gravel
“Built on sand” is a real life saying which means “This won’t last long”. This is true in Minecraft, too.

If you have a floor of sand or gravel, be careful when mining &mdash; digging near sand that isn't supported from below may cause it to collapse. Be wary of placing torches on sand columns which may fall. If such a torch is destroyed, you may find a creeper or other mob which spawned in the darkness.

If you discover a natural cave with a roof of sand or gravel, be extra careful. Even if you don’t choose it to become your new home, as soon as you place or remove a block near it, the loose sand or gravel will suddenly be affected by gravity and fall down, suffocating you, flooding the cave with lava or water and/or allowing mobs to come in from the dark cave/night above.

Beds
Hostile mobs (not just skeletons!) may spawn inside a completely enclosed house when the player is sleeping in a bed if the bed is within 2 blocks of where a hostile mob can spawn. You can prevent this by ensuring that your bed is at least one block away from all walls, or by having all of your exterior walls be double-thick, or by sleeping above ground or underground.

General
  Use a fence or a wall to keep all mobs except spiders and spider jockeys off your lawn.  Use a cactus fence built from two rows of cacti (like this: ) or an alternating row of cacti and flames from netherrack to keep mobs from passing through it.  Build a ditch deep enough that the mobs who fall in can’t get out anymore, which is either filled with lava or streaming water leading to a Mob Farm.  Keep your home area well lit. Mobs only spawn if the light level is below 7; remember that a torch provides level 14 light and this decreases by one for every block away from the torch.  Mobs don't spawn on water. If one or two sides of your home base are open to the sea, you don't need to wall these off. Extend your surrounding wall a few blocks into the sea, and this should be enough to keep the mobs out. (Remember to put a lip on the top of the outer wall, as detailed below, to prevent Spiders.)  Another way to keep mobs out of your house is a staircase that has an overhang.  Try refrain from using ladders to get to your base, unless accessable by opening a door, since mobs can climb ladders. 

Creepers
The single most dangerous mob is the creeper, because of the destructive damage it causes to buildings (they can effectively blow large holes in your structures). However, you are safe within your base if there are creepers outside, as they can't get close enough to you to explode. You might even be able to open your door and kill the creeper with a bow and arrows, as long as you keep your distance!

Also, be aware that creepers can detect you and start their fuse from one block away, even if there is a low wall or fence keeping them away. Ways to avoid this are: build a wall at least two full blocks wide (even if it is made of certain translucent blocks such as slabs, fence or glass); or build your home with all spawning surfaces on all traversable paths sufficiently lit.

Different blocks have different resistances to explosions. Dirt walls will be easily destroyed by creepers, while cobblestone will resist most creeper explosions. Obsidian is explosion-proof, so you can laugh in the face of creepers trying to destroy walls built of this block.

Spiders
Spiders are two blocks wide, which means they cannot move through 1-block wide spaces. Since Beta 1.2, spiders can also climb walls and fences, so you won’t be safe in your walled garden anymore.

To effectively spider-proof your buildings, you’ll have to do one these things:   Build a lipped wall (a wall with overhang, mostly at the top. and  are common wall-top profiles). This lip can have 1-block wide gaps in it, since spiders require a 2 block wide space to climb.  Or be less creative and either completely cover it with a roof or dump buckets of lava around your castle. 

Spiders can climb cactus fences, but they will suffer some hit damage doing so. Therefore cactus fences only reduce the chances and speed of a spider climbing walls. Either make multiple fences or one fence tall enough to kill any spider that climbs all of them. Each fence only needs to be one row of cacti, because while cacti can’t be placed next to each other, spiders are two blocks wide. Use the method in the section for a version which works on other mobs too. (If you prefer a wall for keeping off all other mobs, you can use this cactus fence)

Skeletons
Skeletons are dangerous because they can fire arrows through gaps in your defenses. Wherever possible, fill open gaps with any of certain translucent blocks such as slabs, fence or glass to make windows.

Additionally, build low walls or fences in places where you may be in range of a skeleton, since they will only fire arrows at you if they can see you.

Spider Jockeys
Spider jockeys are dangerous because of their Skeleton mount. They can climb any block and shoot through glass, so beware of them. You must combine your defensive strategies for both skeletons and spiders. They're pretty rare, so anyone would be surprised if anyone saw  more than one Spider Jockey on the same night!

Zombies
Zombies are the simplest mob to defend against. With limited mobility and no ranged attacks, walls or pits will be enough to keep them at bay.