A relay is a device used to switch larger currents or to create isolation. However basically a simple device, it requires some measures to utilize it properly.
There are various of kinds of relays, but in some way, they all share the same kind of components:
- an electromagnet (that is the coil with the core metal), - some mechanical hardware to convey magnetic forces and - a number of contacts.
The relay is operated by letting a current flow through the wire of the electromagnet. This causes magnetic forces to develop and these will become strong enough to alter the position of the movable contact, thus allowing one contact to be broken and another to be made.
Of course, the opposite happens, when the current stops flowing through the electromagnet.
Especially switching off the current flow, brings some special electronic challenges, as the energy stored in the electromagnet during current flow, causes a strong negative voltage across the coil. Any change in the current through an inductor creates a changing flux, inducing a voltage across the inductor. By Faraday's law of induction, the voltage induced by any change in magnetic flux through the circuit is
This means that the voltage across the coil can reach several hundreds (or thousands in a car ignition) of volts, depending on the decay of the current and the time involved.
If you are interested in the physics of the coil, have a look here 
Normally a relay is controlled by a transistor circuit like in the picture below. The large negative voltage during switching of the relay would fry the transistor. That is why a diode is placed, named a flyback diode, that short circuits the negative voltage.
Despite the flayback diode, switching a coil will cause spikes on the power lines and they have to be filtered as they can easily disturb electronic circuits.
Above a representation of a spike on a logic '1' level. It is not hard to understand that the '1' can also be a '0' when seen at the wrong moment. Also logical clocks will be disturbed, false memory addresses and data will be generated.
- One way to suppress spikes is by decoupling the power wires with the correct capacitors. See also the wiki on the subject in Power. - Another way to overcome the effects of spikes is by making high impedance inputs low impedance. Most of the times a simple 10k resistor is enough. - It is also a good idea to separate the power supply of potentially disturbing devices from the processor circuit. An example that needs to be completed is
The relay in the schema above is one of the famous Chinese parts with a transistor. The power of it is drawn directly from a 5 volt supply. This has the benefit of properly closed contacts. A regulator creates the 3.3 volts needed by the ESP and it suppresses the disturbances at the input (in this case the relay) The transistor is required to get the relay back to off position.
You may find more information about relays, solid state relays, possible problems and solutions at the Basics:_Relays page