Could someone illustrate what happens when the motor stops? The current normally flows from 5V to GND (and the electrons from GND to 5V, but this doesn't matter (I think?)), and then it reverses when you switch it off, so current comes out of the top of M1, and then what? I don't get it here.
While the motor is being powered the current flows from Vcc to GND as in this circuit:
When the power is disconnected (such as the transistor in the ground connection in your circuit is switched off) amongst other things (including the collapsing of magnetic fields) the spinning of the motor turns it into a generator. The power generated is the opposite of what was being provided to make it move. As the magnetic field of the coils collapses additional high voltages are also generated.
When disconnected the motor can be represented as a battery connected backwards. In that configuration the current flows from the + of this hypothetical battery through the diode into the - thus stopping any current from going elsewhere, as in this circuit: