The answer is deeply rooted in the theory of compressible fluid dynamics, so for a fully satisfactory take on the matter you might want to refer to a textbook on that topic (such as Thompson or Shapiro). I will try to give a qualitative explanation.
For our discussion here, let's consider a shockwave as the way to abruptly slow down a supersonic flow and recover pressure; abruptly, because, since disturbances cannot propagate upstream in a supersonic flow, the flow has no way to "know" that the disturbance is coming and it isn't able to "smoothly" accomodate it (as it happens in subsonic flow). A disturbance here is, for example, the presence of the airfoil and its effect on the velocity and pressure fields.
Let's have a look at your picture. In a) the flow is subsonic everywhere and we have no particular issues with that (although we might want to consider some compressibility corrections).
Now, the flow accelerates over first portion of the upper surface of the airfoil, so the speed of the flow there is greater than the speed of the free stream flow (and greater than the speed on the lower surface, so we can have lift).
We can see that for free stream Mach numbers $M_\infty$ (ie flight speeds) close to 1 the flow over the upper surface can actually reach local Mach number $M$ of 1 and even above, because of that acceleration.
This is exactly what has happened in b): here $M_\infty$ is greater than the critical value (usually given as 0.8) and there is a supersonic bubble right there in the middle of the upper surface.
The flow downstream, however, is still subsonic, therefore it has to slow from the supersonic condition it reached and to do so a shockwave appears. This shockwave usually interacts with the boundary layer, adding a very high adverse pressure gradient to the one already experienced towards the trailing edge, so the boundary layer will most likely separate, leading to stall.
As we increase the flight speed, more and more of the flow over the airfoil becomes supersonic, also on the lower side, therefore the shockwaves "travel" downstream until, close to $M_\infty = 1$, the whole flow on the airfoil is supersonic, as in d).
As you can see from c), the shockwave on the lower side actually overtakes the one on the upper side. This is because the lower side is usually "flatter", so the shockwave appears later (ie at higher $M_\infty$) since it experience less acceleration (which is due to the curvature), but it also experience less deceleration towards the trailing edge and the local flow will be supersonic over a longer distance along the lower surface.
For even higher flight speeds, a detached shockwave appears in front of the airfoil: the purpose of this wave is to "turn" the flow around the airfoil. As the flow is supersonic, this turning of the flow can't happen "smoothly", but like we said it is the shockwave that abruptly accomplishes it.
The flow behind this curved shock is still supersonic (except for a region right around the nose of the airfoil) so later downstream we once again find the shockwaves which help the flow go back to its undisturbed free stream conditions.
The detached wave significantly increases drag; in some application this is optimal (eg in the atmospheric re-entry of a spacecraft), but it is detrimental for supersonic flight. This is why supersonic airfoils are designed to have a sharp trailing edge, so that the turning of the flow can be accomplished by oblique shocks attached to the edge, resulting in less drag.
As to answer your last question: where does the shockwave position itself? The answer lies in the boundary conditions, that is to say the conditions that have to be restored downstream of the airfoil: these will be the free stream speed and pressure.
Without going into much details about the variation of pressure with velocity in compressible flows, let's just say that the shock position is such that afterwards the flow has enough "space" left to recover subsonically. So, say you have to recover to a pressure of 1 to the freestream pressure of 10 (random numbers with no meaning) at a certain distance downstream from the airfoil: if the shockwave increases the pressure to 6, it will be positioned in such a way that after the shock (where the flow is subsonic) there is enough space for the pressure to increase "smoothly" (subsonically) by 4 and restore freestream conditions at that distance.
The increase of pressure across a shockwave is given by the shock strenght and can be seen as proportional to the Mach number, so at higher Mach the shock is stronger, the pressure recovery is higher and the shock will position further downstream, since the flow will have less pressure to recover subsonically and can do so over a "shorter" distance.