problems with depth

I just started learning OpenGL. Made a simple test program where I render a cube (by drawing 6 quadrilaterals) with each face of a different colour. The problem is that the quads are drawn on the screen in the order they are drawn so the newer faces are drawn above the older ones even if their vertices are behind.
How do I solve this?