Use of backwards facing diode next to motor
When the transistor switches open, energy stored in the motor can cause a back emf and destroy the transistor so the diode provides a current path for that energy to prevent this happening.

The same applies to switching any magnetic component such as a relay, a solenoid or a transformer.