Two elements that distinguish a really funny joke are surprise and superiority. A successful joke also usually features unexpected and smart twists. Good timing and relevance are also extremely important aspects to consider.
The element of surprise is about making the ending or twist unexpected and unforeseen by the audience. This powerful effect can be improved with impeccable timing. A joke, in fact, can be funny at one instant and meaningless at another, if not timed and planned properly.
Some people also use the element of superiority, which triggers when the audience feels superior in any way to the subject of the joke. Many comedians rely on self-deprecating humor to make an audience laugh.