
Left And Right
It may be common sense that determining whether the source of a noise is located to the left or right of us has something to do with the fact the noise would appear louder in the ear closest to its origin.
We might even go as far as to deduct that a sound will be heard by the ear closest to its source slightly before it is picked up in the other ear, thus making it possible to know a sound is coming from the left or right.

But the ears don't merely distinguish if a noise is appearing from the left or right. Much like having two eyes will help us construct a three dimensional visual of the world, having two ears helps us construct a three dimensional soundscape of the world around us.
We can differentiate the distance between the loud hum of a faulty street lamp over the road and the quiet buzz of a bloodthirsty mosquito perched on our ear lobe. We can discern if a noise is appearing from in front, behind, above or below.
Our basic explanation of how the ears work are that sound waves enter the ear and cause the ear drum to vibrate, tiny bones attached to the ear drum send the vibration into a snail shell shaped thing in the inner ear which is full of fluid and tiny hairs, the hairs respond to the movement of the fluid which sends a signal to the brain that is interpreted as noise.

But what cues are the brain picking up on in order to translate the wave information into determining location?
Interaural time difference
If a sound comes from the left it will reach the left ear under a millisecond before the right ear, the brain is able to recognise this slight time difference.

Interaural level difference
Low frequency bass sounds have an omnidirectional tendency and wrap around objects meaning the sound level can be the same in the left and right ear despite whether the noise is coming from the left or right.
High frequency sounds are much more directional so the sound level is picked up as loudest in the ear closest to the source of the noise as the head obscures these frequencies casting an acoustic shadow over the ear furthest from the source.

Head-related transfer function
Now this is where the brain gets really clever and is able to determine quite accurately the direction of a sound source be it from in front, behind, above or below.
The outer ear (the pinna) and the head affect the levels of different frequencies in such a way that the brain can decipher this as the direction of the sound.
In simple terms it is like having a stereo with a bass, middle and treble equalizer. Turning the treble down on a piece of music would make it sound distinctly different, and so it would cutting the bass or middle.
The pinna and head make for subtle cuts or gains in frequencies that are unique to whether the noise is appearing directly above, behind or below, or from any given angle in the audio field.

It is thought that the the brain may even take into account the sound reflected into the ear off of the upper body as a further cue in locating sound.
So how do we detect the distance of a sound?
There is a lot of speculation about how we may detect the distance of a sound source and has much to do with how the reflections of distant sounds differ from near sounds in the time the reflections take to reach the ear, as well as atmospheric distortions to the sound wave or sound quality as it travels through space.
Personally I think it's because of magic.