Why a transistor acts as a switch?

Only because the circuit that its embeded in is designed that way.
Remember, a transistor is basicly an amplifier ...
only if you design everything to go to the extreams will it act as a switch.