diode 1n4007 in fan speed control project
The diode prevents the NPN from blowing up when it turns off with current still flowing through the fan inductance.

A diode in this configuration is called a fly-back diode. The inductance of the fan means the current can not stop immediately when the transistor turns off. With no diode there that would generate a LARGE positive voltage at the top of the transistor, enough to destroy it. The diode provides an alternate path for the current to go as it decays. The voltage at the top of the transistor is then limited to about 12.7V.