And I know it works and that the diode protects the negative voltage from flowing back into the transistor. This is essentially the circuit:
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.