The riddle
When Is a Door Not a Door?
From Riddles and Jokes, 1863
Ajar, or a jar
To be ajar is to be slightly open. Said aloud, "ajar" sounds exactly like "a jar", and a jar is not a door. So a door that is a-jar has, for the length of the joke, stopped being a door. The riddle exists only in speech: written as one word, "ajar" gives the trick away.
It sets up a puzzle about doors, so listeners try answers such as "when it is open" or "when it is off its hinges". The riddle never asks about the door at all, only about a sound.
Two meanings, two words
A door can be shut, open or ajar, the in-between state in which it is open a crack. A jar is a container with a wide mouth. The riddle sets those two ideas side by side and asks the listener to notice that they are said the same way.
In an 1863 book
Riddles and Jokes, an anonymous London collection printed in 1863, gives the answer as "When it is a-jar." The hyphen is the printer's way of letting the reader see the two words hidden in one.
Asking it at a real door
Few riddles can be staged as easily. Ask it beside a door that has been left slightly open, and the answer is standing there when the guessing runs out.