This is the basic implementation of the z-buffer algorithm .
Suppose the depth buffer is a matrix d[100][100];d[][];
for(each polygon P)
{
for(each pixel (X, Y) on the polygon)
{
depth_variable = depth of P at (X, Y);
if(depth < d[X][Y])
{
color_variable = color of P at (X, Y);
set the pixel color at (X, Y) to color_variable;
d[X][Y] = depth_variable;
}
}
}
As we can see while iterating through polygons we only store the new pixel value if the new depth is less (or less than equal to depending on the setting as mentioned by Mr. Nathan Reed) than the stored one in the depth buffer, so it doesn't matter in which order you render the polygons.
The only thing that matters is the fact which polygon has the least depth at a particular pixel.