Humor is a universal human reaction to certain circumstances. Most people would say things are funny because they are happy and fun. However, if one examines what people see as funny, for the most part, these things are those which are forbidden in some manner. This humor comes in many different varieties. There are puns, dirty jokes, jokes about stupid people, nonsense jokes, and shaggy dog stories.
One theory on what is funny is that people find things funny just long enough so that they can block those things out of their minds. Thus humor functions as method of allowing the brain to build censors to block one thought out and keep it from occurring again.
Another opposing view suggests that things are funny due to their forbidden nature, but once they are funny once, or maybe several times, they may well cease to be funny, as they will have removed the censors which had made them funny in the first place.
One analogy of this second theory is the concept of a court jester. The court jester was the one person in the court who could criticize the king and get away with it. He could call the king names, say bad things about him, or ridicule him without punishment. This is because he was seen as funny. However, though these wild comments were laughed at, and passed over, they got into people's minds, and made them think things about the king that they would not have otherwise. This could give the people incentive to riot, or convince the king to mend some of his more tyrannical ways.
Thus the jester could be used as a mechanism for circumventing censors in the Kingdom. Suppose one day the king (who likes chocolate) decrees that no-one in the kingdom but him can eat any of the royal chocolate. No one dares to criticize him. Later that week, the jester performs a pantimime of a greedy noble who hoards everything to himself. Everyone laughs at it, but the king thinks about it for a while, realizes how silly he is being, and decides to share his chocolate.
With humor, the brain is the king. A person can listen to the joke and laugh at it without being threatened, as jokes are expected to be laughed at and not taken seriously. Laughter and humor take down a person's censors, and open people up to hearing things which they might not be willing to listen to otherwise.
This is part of why humor in teaching is so effective. Even if the subject matter is so distasteful that the students have blocked themselves against learning it, if an instructor approaches it with humor, he can usually get the students to learn something, as the act of laughing makes them let down their guard.
In addition, a sense of humor allows a person to laugh at his own mistakes. This is an evolutionary advantage. One can envision a situation in which someone is criticized, gets upset about it, and so ignores the advice, and later dies as a result.