How To Connect LEDs
A standard, one-color LED has two leads. Typically, one is longer than the other. The longer one is the anode ("+"), the other is the cathode ("-"). The anode (the longer lead) should be connected to the positive lead of the circuit, and the cathode should be connected to the negative or ground lead.
If both of the LED's leads are the same length (which sometimes happens if the LED is removed from a circuit or the leads shortened to make it fit), then look for a flat side at the bottom of the LED's case. Look at the LED straight on from the top and you'll see one edge is not round but flat. The lead nearest the flat edge is the cathode, or negative lead.
How To Insert LEDs Into a Circuit
Never connect an LED directly to a power supply; it will self-destruct. Always have a resistor in the path so that the current through the LED can be controlled. This resistor is called a "current-limiting" resistor. An LED by itself will happily take whatever current the circuit has to offer; until it self-destructs. This happens in a matter of a second or less!
How To Calculate The Current-limiting Resistance
Because of the current-limiting resistor, you can control the amount of current the LED is allowed (call it "portion-control"; we want our LED to be on a steady diet so that it doesn't starve and doesn't overeat). The side-effect of controlling the amount of current going through an LED is that the amount of light it emits can be controlled. The less current, the dimmer the light. The more current, the brighter the light. Again, too much of a good thing here, i.e. current, and the LED will blow. Sometimes an application calls for a very bright scenario, and sometimes you want a dimmer LED.
If you have done anything relating to electronics, you will be familiar with Ohm's Law. Ohm's Law states that voltage is equal to current times resistance (V = I * R), where V is voltage (in volts), I is current (in amps), and R is resistance (in ohms). We will use this Law to calculate the size resistor we need for a certain brightness.
When you buy a new LED, the manufacturer will provide some key information about the LED on the package or in electronic format via a PDF file on their web site. Different from a regular diode, an LED can have completely different parameters based on its light color. Different chemical components are used to produce the white, gold, yellow, red, green, blue, etc. colors that are currently available. These have different parameters in normal operation. So, be sure to read the information about the specific LED you are about to use.

In our example, let's assume that the LED "consumes" 3.4 volts from the circuit to run it (should be stated on the package). Let's also say that the LED's maximum current is 20mA (i.e. 0.020A), which is generally stated on the package as well. Finally, we assume we have a standard 12-volt power supply which we wish to use to run our LED. We are going to assume a simple circuit that turns on the LED when the power supply is plugged in to the wall. To prevent the LED from burning out or aging rapidly, we are only going to provide it with 10mA of current (0.01A), which is a good, safe number to shoot for initially.
Here are the steps to calculate the needed resistance to have the LED operate given the above numbers:
where Vps is the voltage of the power supply (in my sample that's 12 volts), and Vdrop is the LED's rated voltage drop. So, connecting a 860ohm resistor between the positive lead of the power supply and the positive (anode) lead of the LED will allow 10mA of current to run through the LED.
You will want to find a resistor whose value matches that calculated number as close as possible. You can experiment with smaller and larger values to achieve the desired brightness (i.e. raise or lower the current from the initial 10mA starting point).
Using LEDs in Locomotives
This is a somewhat dated paragraph since it seems that most manufacturers are now using LEDs as standard equipment for new locomotives. However, you may have some older engines that still use miniature lightbulbs. When those burn out, or when otherwise desired, you can replace them with an LED and resistor combination. Simply use the maximum track voltage and the desired current through the LED to determine which resistor to use.
Even though your new engine may have an LED for its headlights (and/or ditch lights), it may not be of the color that you desire. Again, you can replace them. Just make sure that you adjust the resistor if the LED has significantly different specs than the one you are replacing. The photo below shows three N-scale Kato E8 engines with different 3mm LEDs.
I have, since about 2010, been using SMD (surface-mount) LEDs. These are very, very small LEDs, but have an incredible brightness. These can also be had in various colors. I mounted them into my S-scale RS-1 that I was building, and you can see its brightness in the next photo.
I also used one in the cab roof of that engine to show the interior of the cab.
Other Ideas
You can also use LEDs in structures and in model automobiles.