Just re-iterating and re-wording:
The depth buffer usually works as a shortcut to see if something should be drawn or discarded. It has several options:
* NEVER (don't draw)
* ALWAYS (always draw)
* EQUAL (draw if the values are exactly equal)
* NOTEQUAL (draw if the values are different and store this)
* LESS (draw if the value is smaller than what exists, and store this new smaller value)
* LEQUAL (draw if the value is smaller or equal to what exists, and store the new value)
* GREATER (you get it...)
* GEQUAL (Greater or equal)
Generally games will not explicitly clear the depth buffer. Instead they set the flag to ALWAYS and draw their skybox or draw a distant plane, thus obliterating whatever was stored and setting a new maximum depth.
The depth buffer can be used for other tasks as well as depth. You can use depth images, or pre-computed depth fields, to mix and match 3D worlds with pre-rendered 2D images by drawing a pre-rendered image and telling the depth buffer what pre-computed depths they represent. You can use depth buffers to help compute fog or atmospheric scattering or depth-of-field distortions.
As for how the depth number is determined, there are many methods. You can change the values that get used inside your graphics shaders or compute shaders.
Because of the magic of floating point, smaller numbers are more precise than bigger numbers. The sliding scale of the decimal point (hence "floating" point) means the tiniest numbers have the greatest precision. Each time it floats bigger, the precision drops by half. Floating point precision operates at logarithmic scales.
That means that the nearest items usually have high precision, but distant items can suffer from "z-fighting", where planes that are touching each other will shimmer and shift between the objects as the camera and models moves.
To counter the sliding precision, people have come up with many different ways to compute the depth values.
You can use the simple calculation of (object depth / view frustum depth). That is normally the default that happens if you didn't provide a custom value. Some programs will store the inverse of that distance, termed a 'w-buffer". Some programs will use a logarithmic z value, basically reversing the logarithmic nature of floating point turning it back to a roughly linear scale. Some programs will use different algorithms that suit them better.
The value that you store and compare against is up to you.