How does the phone detect if 3.5 mm jack circuit is closed?
Headphone jacks have extra contacts inside, which act as switches. The the drawing below, pins 4 and 5 are intended for sensing that the plug was inserted. They are not intended for audio signal. When the plug is not present, the switche, which are formed by 2 & 4 and 3 & 5, are closed. When the plug is inserted, these switches are open. The plug flexes 2 and 3 slightly, and they break contact with 4 and 5. You could insert a 3.5mm plastic rod [a dummy] into the jack, which will open the contacts, and the phone might think that earphones are plugged in.
Source: datasheet for a typical stereo jack.
On Android phones, on iOS devices, and on HD Audio PCs, no mechanical switches in the socket are used. Instead, the headphone socket has 4 contacts instead of 3, and accepts both 4-contact headsets and 3-contact headphones. The sleeve of the 3-contact headphone audio jack connects two of the socket contacts together.
One of the contacts is responsible for microphone and usually feeds 1.5-3.3v of voltage through a current limiting resistor (2-10 kOhm), which is necessary to bias a JFET transistor in the microphone capsule of a headset. DC resistance measurement between the microphone pin and the ground pin of the socket can be used to detect the kind of device plugged in - it will be 0 Ohm for a headphone, infinitely high for no device connected, and about 2 kOhm thereabouts for a headset with microphone.
The bias current limiting resistor forms a part of voltage divider network, with the other part being the above mentioned DC resistance. Voltage measurement on the microphone pin is taken to both determine the sound pressure on the microphone (through a 100hz high pass filter thereabouts) and the kind of jack or device inserted (through a low pass filter or noise rejection logic), allowing this design to be implemented without extra parts, if the filters are implemented digitally. Corresponding to the above DC resistances, you will measure about 0V on the microphone pin if headphone is connected, the full mic bias voltage in case nothing is connected, and something in between in case a headset is connected.
Switches in the audio jack like in the answer above were common in older electronics, but are incompatible with headsets and are just too bulky for a high-tech handset.
There have been quite a few answers on this, but still some additional clarifications may be helpful:
yes, quite a few headset jacks include still an insert detect switch, which may also be a signal switch for simple (mono) headsets.
when the insertion is detected by some means, the headphone amplifier may enter a detection mode (see e.g. WO 2006045617 A3 patent application) where the host device detects what is connected (stereo headphones, mono headset with mic, stereo headset with mic, video connector, etc.) and in which order the ground and microphone wires are. This is included in the latest revision of the OMTP headset standard. This kind of detection wouldn't be fooled by a non-connected plug in the original question.
Apple 3.5 mm jacks employ a proprietary device identification chip connected across the microphone line. This performs a handshake when the accessory is inserted and headset controls use this for controls. If an Apple device doesn't find this chip in a headphone it assumes that it is a basic stereo headphone without a microphone or any controls.