http://en.wikipedia.org/wiki/Z-buffer

Basically, a pixel has doesn't just have a color, but also a depth value that describes how far the visible object on the pixel is away.