Minecraft/How the Game Works

Spawning
Rules mob spawning follows:
 * 1) Mobs must spawn within 32 blocks of a player
 * 2) Mobs cannot spawn within 24 blocks of any player
 * 3) Enemies must spawn in the dark

Mobs will begin despawn (vanish into thin air) if it has not been named with a name tag and no players are within 32 blocks. Most mobs (exept those named with a name tag) instantly despawn if they are further than 128 blocks from the player. Mobs do not despawn if they are in a confined space smaller than 25 blocks in all directions. This is extremely helpful when farming animals for meat.

As a general rule, only mobs that can spawn naturally (and can respawn) can despawn (preventing you from going to a village or end city, leaving, coming back and seeing it empty). The mobs that can't despawn include:
 * 1) Villagers
 * 2) ../Npc/s
 * 3) Tamed animals
 * 4) The Wither
 * 5) The Ender dragon
 * 6) Shulkers
 * 7) Pillagers
 * 8) Illagers
 * 9) Evokers
 * 10) Elder Guardians
 * 11) Iron Golems
 * 12) Snow Golems

For these mobs, getting farther away than 32 blocks causes them to freeze until you get near again. It also causes them to be invisible until you get closer.