I once took a course with Greg Dean when he was in San Francisco.

His joke structure process spells out very well how many jokes work.

Source.

As Greg says, “jokes have a structure. It’s not just some random thing that happens.” Let’s take a look at the process, and an example.

He begins with the set-up and the punch, which will create a gap or a step between 2 stories.

He says, “a joke creates what I call a 1st story e.g.”

And continues, “Did you all get a picture in your head? That is the 1st story.”

“…you give them a little piece of information, they create this elaborate story in their head.”

In other words, he says, the audience make ‘assumptions’:

When it comes to joke telling “…the audience makes up a whole load of crap you didn’t say.”

Here is the punch…

Which creates a second story in your head.

As you can see, the ‘punch’ (as Greg prefers to call it, instead of Punchline) takes it in a different direction, with an alternative scene being painted.

So much of this is happening unconsciously, and a skilled joke teller (and writer) knows how to create a gap that leads to a laugh.