I know what its purpose is (to protect the transistor from back EMF of the motor when it is shutting down), but I just can't understand why it is in the opposite direction of current flow.
Well, if the diode was reversed so it would be "with" the current flow, the current would flow through it rather than the motor! Not what you want.
The reason for its orientation is to complete a circuit. The circuit has electrons starting at the inductance (motor, relay coil, etc.) and having to somehow find their way to the other side of the inductance. This is because the external circuit has been switched off (to de-energize the inductance) yet the inductance, as it loses energy, creates its own current flow. Without a path for that current flow (which is in the same direction as the flow that was there before it was switched off), it will find a way through the insulation of the coil, through the air (if powerful enough), and you generally won't like the results. The diode provides a safe path for that decaying current.
--
The Rugged Audio Shield: Line In, Mic In, Headphone Out, microSD socket, potentiometer, play/record WAV files
Close, the diode is to allow the decaying current to flow through a safe, well-defined path. Rather than punching holes in the coil's insulation for example.
--
The Flexible MIDI Shield: MIDI IN/OUT, stacking headers, your choice of I/O pins
Think of the electrons in an inductor as magically acquiring a lot of momentum - they don't want to change their speed quickly and can do damage if you try to stop them dead...