Computer firewalls are named after those barriers in buildings that are supposed to make it hard for a fire to get through a wall into the adjoining room. The principle for the internet isn't to do with fire, but regarding unwanted connections being made to or from your computer.
Ideally, we'd be running computer systems that ignored unwanted connections, but many don't. Many have serious flaws that are hard to fix, if not impossible for many people, so a simplistic solution is to put something else on the system to act as a barrier against such unwanted connections. It doesn't really fix the real problem, but can protect you from such weaknesses.
For instance, Windows design history is based on a system where anybody can do anything without restriction, including remotely, and is only more recently recognising the problem with this attitude. There are plenty of systems still in use where if a malicious person can make a connection to your computer, they can install software to do what they like to it. Suitable firewalling can intercept and negate such connection attempts. This is particularly important for when you're unable to stop your system, itself, from listening to remote connections.
Then there's the opposite problem: You might have software running on your system which tries to make contact with something outside, that you don't want to. Sometimes this is because you have software installed that you actually want to use, but it also has unwanted extra features that you can't disable. And there's also the situation of software that's been installed surrepticiously.
Firewalls use rules to determine what will be allowed. The rules can be based on various different criteria, such as the following, separately and in combinations: