A firewall is a device or program that sits between a computer or network and outside connections, such as the internet, and checks network traffic against a set of rules. Traffic that matches the rules is allowed through, and traffic that does not is blocked. According to the U.S. Cybersecurity and Infrastructure Security Agency (CISA), firewalls can be set to block data from certain network addresses, applications, or ports while letting needed data pass.
Firewalls come in two main forms. Hardware, or network, firewalls are physical devices placed between your network and the internet, and many home routers include one. Software firewalls run on the computer itself, and most operating systems have one built in. CISA notes that firewalls mainly protect against malicious traffic, not against malware you install yourself, so they work best alongside antivirus software and regular updates.
Source: CISA: Understanding Firewalls for Home and Small Office Use
Image: Gateway firewall diagram, by Harald Mühlböck, CC BY-SA 3.0, via Wikimedia Commons.