As far as why a depth dest would happen before or after the fragment shader, there is a section in that wiki link on Early Depth Test as well as an entire article about it here.
When a depth test is performed in the fragment shader itself, it's possible that the Fragment's output depth value is tested against the depth of the sample being written to. There is a line in that article:
If the test fails, the fragment is discarded. If the test passes, the depth buffer will be updated with the fragment's output depth, unless a subsequent per-sample operation prevents it.
This "per-sample" operation means that a fragment shader can be instructed to only perform depth tests on a per-pixel basis and you would therefore want/need it in the fragment shader.