Transistors can be used as a switch. A small base current is used to switch on a much larger collector current.
The diode allows the inductor current to die away slowly preventing a damaging back EMF that would otherwise destroy the switching transistor.
Inductive loads include

