First, it should be stated that many of us have been yawning since we were in the womb. Also, humans are not the only creatures who yawn. Chimpanzees do it, dogs do it, birds do it and even fish do it.
But why all the yawning?
One theory, backed by research, states that we yawn to cool our brains. Brains use up 40% of the body's metabolic energy and, consequently, get pretty hot. When we yawn, we stretch the jaw muscles and increase blood flow to the head, as we increase air into the sinus passages, mouth and nasal cavities. Cooler blood goes up to the brain and warmer blood goes down into the body. Cool air is inhaled and warm air is exhaled.
One study found that 41% of people who put a warm pack on their heads before watching a video of people yawning, yawned contagiously; only 9% of those in the study who put a cold pack on their heads yawned when watching the video.
Similarly, a study using mice showed that before yawning, the mice brain temperatures were higher and after yawning their brain temperatures were lower.
More evidence is found in the fact that we yawn before going to bed and also as we wake up in the morning. Our brain and body temperatures are at their highest just before we fall asleep; as we fall asleep, the temperatures decline. And, as we wake up, the temperatures start to rise again quickly. It's reasonable to say that we are cooling our brains before we go to sleep and after we wake up and this accounts for why we yawn at those times.