Only humans, chimpanzees and dogs have been seen to yawn contagiously. That is, they yawn when someone else yawns first. This contagion is not imitation or copying, but an automatic response to yawn when you see, hear or even think about someone else yawning.
Among the theories are that contagious yawning keeps groups of people in the same state and ready to work together. So if one is sleepy others will feel sleepy too and the whole group can coordinate their routines. Others suggest that it maintains group vigilance, keeping groups alert together. Yawning can occur in response to anxiety so another theory is that contagious yawning is used to warn others of danger.
These group theories gain some support from evidence of a brain connection between empathy and contagious yawning, but really there is no generally accepted theory to explain it.