A transistor is a semiconductor device that is commonly used as an electronic switch in circuits. Transistors are like electronic switches. They are both useful in simple applications, like using a single transistor to allow a Raspberry Pi to control a device that requires a lot of current to operate, and more complex applications, like using billions of transistors for logic in a microcontroller.
Transistors have three main legs: the collector , emitter, and base.
The collector is the leg that you connect to the positive end of your main power source.
The emitter is the leg that you connect whatever part would normally be getting the power to.
For example, if you added a transistor to a very simple circuit with just a battery and resistor, the collector would be connected to the positive terminal of the battery and the emitter to resistor.
Finally, the base would be connected to either ground or the positive end of a different power source. When the base of the transistor is connected to the positive terminal of a battery or ground, depending on the type of transistor, current can flow through the collector and base and the circuit will run normally as if there was no transistor at all. When the base is not connected to what it should be, no current will flow through the circuit.
There are two main types of transistors: PNP and NPN transistors. Without getting too detailed, the two types of semiconductors used in these transistors are called p type semiconductors and n type semiconductors. (The name is determined by the sequence of the types of semiconductors.)
The base of a PNP transistor needs to be connected to the positive terminal of a battery to work and allow current to flow through the circuit and an NPN transistor needs to be connected to ground.
The base allows transistors to be used as a digital switch. If you can only control a small amount of current, like the output of GPIO pins, you may not be able to turn on a device that requires a lot of current directly. However, you can keep the motor connected to a larger power source at all times and then connect the GPIO pin to the base of an NPN transistor. Then, you will be able to control whether or not current flows through the circuit with a much smaller power source.
Here is the schematic symbol of a PNP and an NPN transistor: