Why Laughter and Humor Evolved (my guess)

Laughter evolved because it provides quick relaxation after removing a possible threat, thus facilitating bonding between people who have collectively removed the threat, leading them to collaborate more in the future. Humor exploits this response by setting up and then quickly removing perceived threats, such as the surprise endings of jokes.

Laughter to Facilitate Bonding with Compatible Bandmates

Imagine that you're roaming the savannah with three fellow evolving humans, and a snarling leopard suddenly leaps out and threatens you. You're terrified, but the four of you working together manage to drive the leopard off. Due to your successful teamwork, it would probably be a good idea to continue hanging out with this particular crew, since you apparently work well together and can help each other survive and reproduce. Continuing this collaboration would be more likely if you were to emotionally bond with your teammates. Unfortunately, at the moment you're too rattled to bond effectively. Bonding requires relaxation so that you're no longer "on guard" and can open up to others.

You could just wait a while until you've gradually become relaxed again, but by then your team likely will have separated. And you're more inclined to bond with them while the image of your combined success is still vivid. It would be handy if you were able to relax very quickly so that you could bond while the team is still together and celebrating your triumph. A few slow, deep breaths would help, but right now you're too excited and your chest muscles are too tensed up for that to happen naturally. Fortunately, natural selection has come up with a response that causes you to quickly relax in situations like this, where bonding is important. It's known as laughter.

The Mechanics of Laughter

Laughter forces you to exhale deeply, by triggering a series of short, uncontrolled, and forceful exhalations. It also blocks your trachea between these exhalations to prevent you from inhaling in the meantime, until you have exhaled fully. When the forced exhalations finally allow you to inhale again, a deep inhalation to recover completes an involuntary deep and slow breath, resulting in relaxation.

On chasing the leopard off, you will likely find yourself laughing in glee as the threat has suddenly gone away, leading you to quickly relax and bond with your teammates.

Most laughter is also quite audible as well, though vibrating your vocal chords is not necessary to force slow, deep breaths. Perhaps someone else's audible laughter is a clear signal to you that they are having an uncontrolled (and therefore sincere) inclination to bond with you. This removes any worry that you might have that they may be trying to con you by just acting like they like you, and the sudden relief can lead you to laugh yourself. This makes laughter "contagious".

In addition to deep breathing, your leg muscles become spastic during laughter. This can cause you to stagger into your comrades, where the direct skin contact further intensifies the bonding effect.

Laughter Results Whenever a Possible Threat Suddenly Goes Away

Laughter lets you bond with others when you have collectively removed a threat, thereby increasing your tendency to band together. This increases your survival odds, so the laughter response has been selected for in this type of situation. And because natural selection often over-generalizes as it stumbles onto responses that happen to increase the reproductive rate, in this case it has generalized the laughter response to be triggered by the relief that you feel any time a possible threat has suddenly been revealed as no problem.

How Humor Removes a Possible Threat Via Surprise

Laughter is commonly associated with humor, which has little to do with driving off leopards or other threats. Humor is actually just one type of stimulus that induces laughter, and it hooks into the rule of the suddenly removed threat in various ways. One way is through surprise. (Many people have noted that much humor involves a surprise.) Any surprise involves a possible threat, because there is always a moment where you have noticed that something unexpected has just happened, but you haven't yet figured out what it is. Since many surprises are in fact threats, such as a leopard springing into view, you can't afford to wait until you've figured out the nature of the surprise, and then to respond quickly only if needed. Instead, it's important to prepare for a fight or flight response as soon as you are surprised, just in case the surprise turns out to be a threat. If you then see that there is no threat, it will still trigger the laughter response that results whenever a possible threat has suddenly gone away.

In a typical joke, for example, there is a punchline that suddenly makes it clear that various assumptions that you've made about the story are completely wrong, and that the story elements actually fit together in a completely different way. This is initially surprising and perhaps disturbing, but once it registers that the surprise was simply due to "getting" a harmless joke, then the possibility of a threat from the surprise has suddenly gone away, triggering laughter.

Another type of humorous surprise is a deadpan statement that doesn't really reflect your beliefs. There is a brief moment when the listener notices that the comment is surprising, before they realize that you are being sarcastic, quickly triggering relief.

How Humor Removes a Possible Threat in Other Ways

Humor can remove potential threats in ways other than by setting up surprises that turn out to be non-threatening. For example, much humor involves admitting something stupid that you've done, or some embarrassing trait that you have. Often this is something that's true for most listeners as well, and they have worried about what might happen if they admitted it. When you come out and admit it about yourself, and nothing terrible results from that, it makes it clear to listeners that they have overestimated the negative consequences of admitting it about themselves, and the relief triggers laughter. Such self-deprecating humor is especially effective when done by people who are seen as very successful. If even a successful person has a particular foible, then it's not much of an embarrassment for you to have it too.

A similar way that humor can burst a perceived threat is by exposing bullshit. You may be harboring a complaint about the same bullshit, but you haven't expressed the complaint for fear of aggression by those who disagree, or of being seen as someone who holds unusual (and therefore likely inappropriate) views. When someone else comes right out with the same complaint, perhaps even exagerating it with hyperbole for humorous effect, it demonstrates that doing so is acceptable (or at least that it can be gotten away with). And when a whole audience laughs, it becomes clear that your view that the comedian dared to express is not at all unusual. "Potty humor" may be the most basic example of this type of humor, by defying the bullshit rule against joking about such "degrading" things, even though bodily functions are a basic and important part of life.

Even a joke that has a surprise ending can quickly remove a possible threat in a non-surprise way as well. When we find ourselves in a situation that we can't make much sense out of, that's scary because we can't even figure out if there is a threat or how we should respond to it if there is one. A joke-teller can create such a situation by setting up a world that doesn't seem to fit together. Getting the joke suddenly makes everything make sense, and the relief of that adds to the usual relief of realizing that the surprise ending is not a new threat. Getting a joke may also suddenly remove a fear that we weren't going to get the joke, and appear stupid!

Ridiculing an Enemy Makes Them Less of a Threat

If you make a humorous remark that demonstrates that the enemy is rather incompetent, then the enemy suddenly appears to be a less formidable threat, and the relief results in laughter. And if the joke is perceived as being particularly clever, then the foe may seem like even less of a threat because they are surely no match for us clever folk who all got the clever joke. A quick realization that the foe is not much of thread results in laughter.

But Laughter for Intimidation Is Not True Laughter

Humor that ridicules an enemy that is not present should not be confused with laughing "at" someone who is present, to intimidate them. The latter is intentional (fake) laughter, and not true involuntary laughter at all. The intimidation effect works because we understand that the object of laughter is regarded as a non-threat. So this sort of forced laughter for intimidation is a secondary phenomenon that developed only after we had a partial understanding of natural laughter.

Slapstick could be regarded as laughing "at" others in a natural way. But the laughter results only when the initial shock of seeing someone harmed is quickly followed by the conscious realization that they're not really being harmed. Seeing a foe do something stupid could spark true laughter from seeing that they are not a threat, or forced laughter to ridicule them, but not true laughter to ridicule them.

A Note About Tickling

Tickling is a source of laughter that also occurs in other species, and therefore may be the original source of laughter. The main response to tickling is to try to remove the source of the tickle. This reaction probably was selected for because it can quickly remove insects and other parasites before they manage to bite us, when we notice the tickle sensation of them crawling on our skin. We are therefore especially ticklish on the bottoms of our feet, for example, where we are especially likely to make contact with other creatures by stepping on them. We are also very ticklish under our arms, where parasites are likely to take refuge in that warm moist area.

But if we see that the source of the tickle is not a possibly harmful creature, and is instead another human who's having some fun with us, only then does the tickling result in laughter. Perhaps a mother is compelled to tickle her infant because that gives the infant practice at accurately swatting parasites. The infant laughs because it then relaxes and further bonds with its mother who is providing the valuable training.

Subverting the Drudgery of Life that Has Evolved Only to Reproduce Genes

Natural selection leads us to work constantly to maximize our reproductive rate, rewarding us with pleasure only for putting in the work. Humor subverts that dicate by exploiting pleasure responses that evolved for other reasons, providing pleasure for free. An oasis of pure joy amidst the daily grind of life.

[ The narrative above includes some ideas that have been widely expressed by others, plus many ideas of my own to fill in the holes and for clarification. In particular, there seems to be a dearth of explanations about why the particular physiological response of laughter is involved in humor, as opposed to some other response like jumping up and down while holding our breath, so I tried to address that question. ]
More Random Cruft from Ken