solenoid control valve
Once the ON current is established, the inductance will try to keep the current flowing. As a result, if you turn the transistor OFF very quickly, the inductor will cause a voltage spike (which, if it works by, for instance, destroying the transistor, will cause the current to continue flowing). For a transistor voltage greater than Vcc, the diode will be forward biased, and the transistor voltage will be clamped to a bit above Vcc.

This is called a flyback diode, and is a cheap and easy way to protect the transistor.

For what it's worth, although it's cheap and easy, it gives the worst performance in terms of speed. This is precisely because it clamps the voltage at Vcc. The appropriate equation is $$V =L \frac{di}{dt} $$ where di/dt is the rate of change of the solenoid current. Since what you want to do is change the current to zero, the lower the clamping voltage the slower the response. While this is almost certainly not a problem in your case, there are other approaches available which will give better performance.