When does OpenGL divide by W? Is this done automatically within the vertex shader? It is my understanding that the vertex shader outputs the final vertex position, so I presume no transformations can be made after this point...
It's somewhere between the vertex and fragment shader (after the geometry shader and right before the fragment shader if you want to be specific). Since the fragment shader runs for each drawn pixel, the positions have to be divided by W by then. The division is automatic, you don't need to do it yourself.
Even if you wanted to, doing transformations after this is simply not possible. The fragment shader already has a set of pixels it needs to draw, nothing can change that.