How does a transistor work as a switch explain with diagram?

A transistor operates as a switch by operating it outside of its linear range.

Basically, if you apply less voltage across the base-emitter junction than required to turn in on, then it is off, and if you apply more current through the base-emitter junction than collector-emitter current divided by hFe, then then it is on and saturated, fully outside of its linear range.