How can I hear what's up/down and front/back?

I can determine if a sound is coming from the right or left because of ears on both sides of my head. How do my ears determine if a sound is up/down or front/back?

The intricate shape of your ears changes the frequency response of sound coming from differing angles, and your brain can reverse-engineer the direction from this.

Though it can be fooled. Have someone sit with their eyes closed, away from any helpful wall echo. Snap your fingers in front of them at an angle, and have them point where you are. Then go perfectly behind them and snap, and they’ll almost always point straight in front of them.

Oh, and yes, echoes and also are part of it. Your brain acclimates to the audio space you are in.

The technical term is head related transfer function HRTF.

The HRTF is defined by your ears (and pinna), the shape of your head and to a limited extent upper torso. The pinna add the icing on the cake, adding enough additional information to get you height information.

In stereo recordings there are three common approximations, the Jecklin disk (simple disk set between two microphones), Schneider disk (adds a sphere that approximates more of the head), and dummy head (model of human head and usually very accurate pinna and ear with microphones in the ears).
Only the dummy head really provides enough fidelity to a HRTF to get you a proper binaural immersive experience with height information. The Jecklin disk makes for a nice headphone experience that also sounds good in speakers. The others have enough weirdness in response that they are not so good in speakers, and are headphones only.

The physical blocking of sound and differential distances travelled by sound leads to comb filtering effects that vary with both altitude and azimuth angles. So stereolocation isn’t just a matter of balance of sound intensity between ears, but at which frequencies the sound is relatively louder and softer. The exact balance frequency response can thus provide a good clue as to the 3D location.

To add. The other thing we do that makes location work is move our head. So we both sample the sound field at slightly different locations, but change the effect of the HRTF with respect to the source. Thus the frequency response anomalies change with head angle which further informs location.

Similar to how we move out heads to see through leafy branches, the mesh in the microwave oven door, and peeping out miniblinds.

When I think about this type of thing, the staggering ability of the mind becomes apparent. I think about what it would take to, say, write software that compares a few sets of sound or image data, in stereo, to locate or identify objects in free space. And then do it in realtime. Then, expand to infinity analog data sets. Add noise every step of the way. Reallocate memory stack to prioritize life support and threat assessment systems. Must compile for use in robin-seeks-worm wetware.

Through lots and lots of experience. What everyone else has said about shape, echo, etc., but keep in mind that you don’t intrinsically interpret this. Your experience, starting from a baby, lets you learn how things sound behind or up. This is why your directional sense of hearing can be fooled when you’re in a different situation.

Well, it would be completely impossible to code something like that from scratch, by hand. But you can get there by harnessing the power of natural selection, using a neural network. Which, if you think about it, is the same principle by which we gained this ability.

Do you have a cite for this claim?

Even if human babies don’t turn their heads towards sounds for a couple of months, keep in mind that a human infant is, compared to other primares, born very prematurely (an adaptation we have to fit our large heads through a structure optimized for bipedal locomotion), so not every behavior a baby develops after birth is necessarily learned.

It seems to me that children born deaf and later given cochlear implants never really learn to hear properly. Similarly, I remember reading about a man born blind owing to congenital cataracts. As an adult, he had the cataracts removed, but he could never learn to make any sense of what he saw.

Turn your head sideways.