What is the purpose of the diode in this diagram?
A motor is an inductive load. Inductors “don't like” when current changes suddenly. When that happens, they generate sometimes very high voltages in the reverse direction. It's because when current flows through an inductor, the inductor can store some of its electrical energy, and when the current stops flowing the energy has nowhere to go, so the voltage rises suddenly for a very short time. Such an impulse of high voltage can damage other electrical components in the circuit. The diode lets the voltage spike discharge, because it has polarity opposite to the supply voltage, but blocks the supply voltage from flowing through the diode creating a short circuit. Afrotechmods has a great video on this subject ("Inductive spiking, and how to fix it!").