Data flows into and out of devices through what we call ports. A firewall is what controls what is - and more importantly isn't - allowed to pass through those ports. You can think of it like a security guard standing at the door, checking the ID of everything that tries to enter or exit.
For most normal computers or home networks, the firewall should allow very little, if any, inbound traffic. There's rarely any legitimate reason for other devices to need to connect to your device, or home network, unsolicited.
What kinds of firewalls are there?
Firewalls can be either software or hardware, and most likely you're being protected by both.
The router (sometimes called a "modem") that brings the internet from your internet provider to your home or office is usually a hardware firewall. And your computer, whether it's running Windows or macOS, most likely has a software firewall running.
Learn more
Firewall and network protection in Windows Security
Advanced: Best practices for configuring Windows Defender Firewall