It protects the transistor. It prevents the inductor kickback voltage. inductors have the property V = L di/dt, it is not possible to turn off the current suddenly, since that would imply an infinite voltage across the inductor's terminals. What happens instead is that the voltage across the inductor suddenly rises and keeps rising until it forces current to flow. When the transistor switch is opened, the inductor "tries" to keep current flowing. The best solution is to put a diode across the inductor. The diode must be able to handle the initial diode current, which equals the steady current that had been flowing through the inductor. When the switch is on, the diode is reversed-biased and during turn-off the diode goes into conduction, putting the transistor switch terminal a diode drop above the positive supply voltage.