This type of joke got you believing one thing, and then as I reach the punch line your belief was shattered and the punch sent you into direction of thinking. This is one of 6 Comedic Mechanisms used to produce laughter, they are Surprise, Recognition, Superiority, Shock Humor, Aggression, and Skill .
The First Comedic Mechanism is Surprise. This is used by such comedians as Gallagher, he takes fruit and smashes it with a hammer. How it works is that we assume nothing is really going to happen when all of the sudden something we didn't expect happens and because it's such a surprise to us, a negative wave surges through our brains to produce laughter.
One of the most common mechanisms is Recognition. This is used by a myriad of stand-up comedians and writers. It's the process of taking everything things, ideas and items and making a common, comic truth about them. Such as Jerry Seinfelds Ovaltine Joke: What is the Deal with Ovaltine? The jar isn't oval, it's round. The cup is also round. They should call it Roundtine.
Superiority is the way that many Americans like their jokes. We like to feel smart. Stupid Characters and situations set up comically are in this category. For example, when you see a banana peel and a idiotic person walks along, you think that that person will slip and fall and be hilarious however I am not a stupid person. (Slips on brain).
Shock Humor is the currently the most popular mechanism used in American culture. Shock humor is considered one of the lowest types of humor, but can be quite funny. Jokes such as: How does Helen Keller's parent punish her? They glue doorknobs on the walls. You"d have to be blind to not see that one coming. Truly awful, but somewhat funny. The appeal of these jokes, like the appeal of popular horror "hack 'n' slash" films, is to allow the mind to cauterize itself against semi-believable nonexistent horrors in preparation for the problems of everyday life.