The good guys get through and get what they need, but the bad guys don't. A firewall is a computer that controls and restricts traffic between your internal network and the Internet. It is what stands between you and the outside world. Firewalls protect the entire internal network from attack by forcing all Internet traffic going to and from the internal network to pass through it. It monitors and filters traffic and prevents anything not allowed in the security policy to pass through.
Firewalls come in many different flavors and can be configured to perform many different functions.
You can choose to permit only email through them, which protects the network against any attacks other than those against the email server. Others provide less strict protections and block only services that are known to be problems. Generally, firewalls are used to prevent unauthenticated interactive logins from the outside world. This, more than anything, helps prevent vandals from logging into machines in your network. More elaborate systems block nearly all traffic from the outside to the inside.