Summary: A relay is a mechanical switch which can be actuated electrically from a remote location.
A Relay is an electrical component where one electric circuit can switch one or more separate electric circuits. A typical relay consists of a coil which when energized becomes an electromagnet, attracting a metal armature on which the switch contacts are mounted. One use is for safety, where large currents and voltages can be controlled remotely using a much lower voltage to operate the relay.
A common example is the starter solenoid in a car. Heavy wires run to the relay and then to the starter motor. Low current, light gauge wires connect the solenoid to the battery via the ignition switch. This allows heavy currents to be switched at a lower cost, without running heavy wires to the driver's position, and using a heavy-duty switch to control the operation of the starter. It also isolates other circuits from the starter motor circuit. (It may be called a solenoid, in some cases it is a relay, in others it is employed as a solenoid.)
Applications with DCC
Small reed switches are used in some locomotives for programming purposes. Unlike a relay, they lack an electromagnet. The magnetic force needed to close or open the switch is supplied by a small external magnet, on a wand or similar tool. Some Atlas locomotives equipped with QSI decoders use this device. Passenger cars with battery powered lighting may also use a reed switch to control the lighting.
Reed Relays are also available, they include a coil to operate them.
A typical relay only requires an operating current measured in mA, for example 100mA. But the contacts it operates can carry much larger currents, ten to a hundred times larger. For example, a stationary decoder with a limited output current can be used to operate a larger current accessory if a relay is used.
Relays used in model railroads typically have low voltage coils, from about 5V to 16V, but the output can be used to switch higher (or lower voltages). Using them to switch household mains voltage is also possible but you should be extremely careful mixing mains voltage with model railroad voltages. If you do, this should be in a separate enclosure away from all your other circuits and grounded if it is made of metal.
In this type of application, a relay is often called a "contactor".
A relay can be used for protection. A common application is a fail-safe, when power is disconnected from the coil the circuit is opened. This could be used to disconnect a power district containing a lift out or removable track section, and the NC contacts used to implement a Braking District. When a train enters the district and it is not safe to do so, it will stop either due to lack of a DCC signal or the presence of a broadcast "Stop" command.
Relays can operate on DC and switch AC circuits, or vice versa. In fact, all combinations of AC or DC coils and AC or DC contacts are possible. This allows the DC output of a stationary decoder to operate an AC accessory.
Note that relays specifications differ depending on whether the supply is AC or DC. Coils are usually specified one way or the other. Contacts are usually specified both ways and are typically rated higher for switching AC voltages and currents.
A relay only requires one operating circuit but can switch multiple circuits and each circuit can be both normally open and normally closed. This can be used to increase the number of switching circuits on a turnout motor. For example, a Tortoise Slow Motion Switch machine has 2 SPDT accessory switches but by using one of them to switch a DPDT relay you can increase that to 3 circuits.
There is no electrical connection between a relay's operating coil and contacts and therefore the two circuits are completely isolated. This allows one circuit to operate another circuit that cannot share a common ground or common return.
Voltage Spike Suppression
When the power is cut to an inductor, such as the coil of a relay, the coil will attempt to replace the energy supplied by the power supply. This will cause a voltage spike to appear, which can damage the circuits driving the relay. To prevent this a reverse biased diode is placed in parallel to the coil, providing a path for the current to flow from one terminal to the other. See the image with the relay driven by a logic circuit for an example.