We use resistors in electronics to protect components from the electricity that gives them life - especially the part they do not use: that is why the resistors will work to dissipate. And while things have gotten better in the last few years, yes, LEDs do burn.
Resistors come in several resistance values, measured in Ohms. You pick the appropriate value in Ohms for the resistor based on how much energy it needs to dissipate. And now for the big question: how do you choose the correct value for the resistor you need?
"Yes, there will be maths."
To choose the appropriate resistor for your project, you're going to know three things:
For example, let's check the details from a basic Sparkfun red LED. From the product page's Features:
And just like that, we have all the information we need to do our maths - because yes, there will be maths! - and choose our resistor with the following magic formula:
Resistance (in Kilohms) = (Power supply Voltage in Volts - Voltage drop in Volts)/Desired current in LED in Amps
First, we replace all the terms in the formula with the information we know.
Resistance (in Kilohms) = (9 - 1.8)/16
Next, we substract the voltage drop value from our power supply voltage.
Resistance = (7.2)/16
Finally, we divide the leftover voltage value by the desired amperage in the LED.
Resistance = 0.45 Kilohms, or 450 Ohms
And that's it. You might not have the exact resistor you need in your kit, but that's alright: just make sure to select the next resistor value that you have instead. For example, you might not have a 0.45 Kilohms/450 Ohms resistor, but you might have a 550 Ohms resistor instead: use that.
We also made a small simulation in TinkerCAD so you can check out what happens with - and without! - a resistor for yourself.
Resistors and electronics - complex topic. Here are a few more links to help you figure them out.