The central idea to the operation of a transistor is that a little bit of current flowing between the base and emitter causes a larger current to flow between the collector and emitter.
A transistor is essentially an amplifier. In fact the initial tests of the first transistor involved hooking a speaker to its output and hearing that it was louder than the input. Every modern piece of audio circuitry has transistors at its heart amplifying signals.
A transistor will act linearly if the base-emitter current is within a specified range (depending on the model of transistor). That means that the collector-emitter current will be some multiple of the base-emitter current. That multiplier is called the transistor's gain. For example, the gain of a 2N3904 (a very common small transistor) is typically around 100. The gain can vary between specific parts even of the same part number. Additionally the exact value of the gain depends on the collector-emitter current. The result is that the value of the gain isn't all that important. The key thing is that it's there, and it is often a couple orders of magnitude, especially with fairly high collector-emitter currents.
Another nice feature of transistors is that they can be driven into cutoff and saturation. When this happens they stop acting linearly and act non-linearly: in cutoff they act like an open switch, and in saturation they act like a closed switch. This switching behavior is incredibly useful, and is the foundation of digital electronics.
Using a transistor as a switch has some advantages;
- A circuit that can provide only a small current (e.g. from a GPIO pin) can control a much larger current (e.g. a motor).
- Switching state happens very quickly (the 2N3904 takes at most 50 ns - about 5 hundred-thousandths of a millisecond). That's a general purpose part; some are much faster.
- Because of the above point, transistors can be switched off & on at a very high frequency... high enough to make modern computer speeds possible.
- A transistor switch can be controlled by a pulse-width modulated (PWM) signal, something that a mechanical switch can't do.
- Transistors don't bounce like a mechanical switch.
- A single control signal can control multiple circuits as each requires only a small current.