Almost all mobs will stack. The only ones that don't are Bee, Ender Dragon, Phantom, Villager, and Wither. We need this to preserve server performance, and it can also be used to your benefit.
Max stack size is 32.
Mobs will attempt to stack if there are at least 3 of the same type (unless other conditions are present) and if they are within a 5 block radius of each other.
Mobs wearing armor or holding something will stack with other mobs of the same type. When it merges with the stack, you may not see it as the top 'layer', but will eventually see it if you kill mobs (or peel off another way) in the stack.
A mob will not stack with another mob of the same type if:
You already have one on a lead
The mob is an active raider during a raid
It is a different color (applies to Axolotl and Sheep)
It is a different age
It has a saddle on it
It has horse armor on it
It has a chest on it (applies to Donkey, Llama, and Mule)
Stacking does not effect breeding. If you have 32 chickens in a stack and right-click with seeds, it will take 32 seeds from you (or as many as you have if you have less) and breed accordingly.
Babies will peel off when adult mobs breed.
In general, you cannot kill a whole stack at a time. The only damage that will propagate throughout all mobs in a stack is by magic damage (potions), and the only time the whole stack can die is by falling.
You can peel off certain mobs from a stack depending on if you can (and how) you can interact with them.
Ways to peel off a mob:
Dye the collar
Babies will peel off if it has just been bred
Items dropped on the ground will stack with nearby items of the same type and name, with a max stack size of 250,000 and a merge radius of 4 blocks.
If 2 items are the same but have different names, they will not stack.