A yawn is a very deep breath and the usual cause of it is that we are tired and our breathing has fallen below what it needed, so we are trying to make it up quickly. The yawn that comes when we see someone else yawning though is due to what is called suggestion.
One example of suggestion is when we become afraid because we see someone else showing fear. Another is when we laugh because everyone around us is laughing. The power of suggestion makes a yawn every catching.