How do firewalls work?

Diagram of a gateway firewall placed between a local network (LAN) and a wide area network (WAN)

A firewall checks network traffic against a set of rules and blocks traffic from addresses, applications, or ports that are not allowed. It can be a hardware device or software built into your computer.