A bipolar junction transistor (either PNP or NPN type) can be used as an analog amplifier or as a digital switch. These days, it is probably much simpler to use an operational amplifier to achieve amplification of analog signals instead of building an amplifier using individual transistors but there are many situations where using a transistor as a switch may be useful. I give an example at the bottom of this post.
The schematic below illustrates how to use an NPN-type transistor to switch an LED. In the schematic, the terminals on the transistor from top to bottom are called the collector, base, and emitter. Look at your transistor’s datasheet to match these terminals to the transistor’s pins.
Biopolar junction transistors are current controlled, meaning a current from the collector to emitter only flows if there is also a small current flowing into the base. This is the essence of how the switch works. Connect the transistor’s base to a digital output of your MCU and when the output is low, the switch is open and current will not flow through the LED. When the the digital output is brought high, a small current flows into the base, the switch is closed, and current flows through the LED. the resistors are present to limit the current through the LED and the base.
You may wonder why you would use a transistor at all to switch the LED. Well, an LED doesn’t draw much current but if you are switching a device that draws more current then the MCU may not be able to provide it but the transistor connected directly to a battery will be able to supply it.
If you are switching an inductive load (a motor or basically anything that oscillates of spins), make sure to put a diode in parallel with the inductive load pointing from ground toward Vcc to protect your circuit from the voltage induced by the inductive load.
Why would you want to use a transistor to switch a small current load like an LED? Suppose you only want the LED to light up if two separate things occur. Then you can replace the Vcc in the schematic above with another digital pin on your MCU. Then your LED will only turn on if both digital pins are set high. This is like a rudimentary AND gate.