First of all, having two ears doesn’t just let you tell if a sound comes from right or left, your ears can [locate the sound](https://en.wikipedia.org/wiki/Sound_localization) source anywhere around you horizontally. To figure out how, you can draw lines from the source of the sound to both your ears, and realize that a longer line to one ear means sound will arrive at that ear slightly later. And your brain can calculate the minute difference in “lateness” and figure out WHERE the sound source must be.
For detecting up and down and forward vs. behind you, the [ear cartilages](http://www.earwellcenters.com/wp-content/uploads/2018/09/ear-anatomy-infant-.jpg) play a role of bouncing the sound into your ear canal but dampening some of the frequencies. So your brain detects the slight absence of high pitch or low pitch sounds that marks the difference between sound coming from in front of you (and reflecting off the cartilages) vs. sound coming from behind you (and having to pass through the cartilages).
Latest Answers