Digital devices need to interface with mechanical relays and LED's from
time to time. Below are examples of how TTL and CMOS digital
outputs are usually connected to relays and LED's.

Figure 1. A
Mechanical Relay Driven by a TTL or CMOS Output
Figure 1
shows how a
TTL or CMOS output may be used to control a mechanical relay.
Actually, the logic gate is just driving the base of the transistor, not
the relay. It is the transistor that energizes the relay. A gate output
of 'high' turns on the transistor, energizing the coil of the relay.
A gate output of 'low' turns off the transistor, causing the relay to
deactivate. Since the relay coil is an inductor, it will generate a
large voltage across the transistor if the latter turns off while
current is flowing through the coil (an inductor opposes an
instantaneous change in current). This inductive kick can destroy
the transistor. To protect it from damage, a diode is placed at its
collector as shown in Figure 1, so that any large voltage appearing at
the transistor's collector will cause the diode to conduct, shunting the
energy towards the positive supply.