Minecraft wiki entry for Animal_farming, associated text: Go to the opposite side of the pen as the door you intend to use, and pull out the appropriate food, to gather the animals there. Then switch items (putting the food away) and quickly run to the gate. Since they're no longer following you, the animals will not be fast enough to get to the other side of the pen when you do. This gets more difficult with large crowds, so other methods can be necessary.
If you put a carpet on top of one of the fences, neither the animals nor hostile mobs can jump on it, but you can.
A double gate can provide an "airlock" to give you a chance to intercept any animals that follow you through the first gate.
Escapees become "volunteers".