First, the 3rd part, the punchline, needs to be as funny as possible, although simply using a less funny gag as a rule of 3 punchline will often make it funny... or funnier than it would have been on its own.
It helps if the other two parts are funny too although they don't need to be, as long as the punchline has the necessary
element of surprise.
Many jokes are enhanced by using a variety of joke-types / techniques within them, as does the rule of three example which follows.
Step 1: Think up your punchline first
Step 2: Think up ways of diverting your audience away from the punchline like a magician misdirecting attention from his sleight of hand
Step 3: Delay the surprise once
Step 4: Delay the surprise twice
Step 5: Zap! Slap in the punchline.
Let's say you want to create a joke about Ellen, who is always late and always has a ridiculous excuse ready.
You then need to consider what exactly makes her late, then lampoon (take the mickey out of) it.
Let's say it's her make up and clothes. I can see some humour in these things taking her ages, then her taking hardly any time at all to come up with a fleeting excuse.
This lead me to a punchline, such as it is, indicating that she habitually spends only a few seconds in making up an excuse.
This train of thought results in this:
Ellen always takes 3 hours and 3 seconds to get ready: one hour to put on her make up, one hour to choose an outfit and three seconds to make up an excuse for being late.
Coming up with this joke we've used several aspects of comedy writing, namely:
Thinking about the punchline first
Double-delay and misdirection
Structure within a structure
When to think about the punchline
Although I decided more or less what the punchline was going to be, some writers I know succeed quite well by writing the set-up of a gag first, then put the punchline in afterwards.
I find my method works for me.
Is provided by the rule of three format
This lies in making the contents of delay 1 and delay 2 interesting in themselves
In joke-writing it is generally a rule that you deliver any punchline before the audience thinks of it themselves. We must do what we can to make it a surprise.
Even on its own, exaggeration can be funny and it can enhance laughter when forming part of other jokes.
In our joke about Ellen, everyone will recognise Ellen's preparations being a much-exaggerated reflection on the real-life ways of women, but if she was going to invent a serious excuse she'd probably take longer over it.
In the interests of humour we are entitled to stretch and shrink timescales in any way we like.
As Sean Lock once said, "This isn't reality, you know... I'm a comedian. I make it up."
Structure within a structure
By this I mean the symmetry of the '3' hours and '3' seconds, but the joke would probably still work if it said the following:
Ellen always takes 3 hours and 7 seconds to get ready: one hour to put on her make up, one hour to choose an outfit and seven seconds to make up an excuse for being late.
To me there is something pleasing to the ear in finely judging / balancing the words you use.