Packet filtering is a commonly-used defensive security measure. In general, packet filtering examines certain important parts of IP, UDP, and TCP packet headers to decide whether to forward such packets into the network or to accept them on a host.
Firewalls provide even more protection. Although simple packet filters are often called "firewalls" a true firewall usually consists of at least two packet filtering boundaries and a "bastion host" that proxies certain traffic between the hosts behind the firewall and external hosts, while preventing external access from external hosts to any host but the bastion host and even from the bastion host to any hosts internal to the firewall. This setup is much more complicated to set up and manage but provides a much higher assurance of protection for hosts behind the firewall.
This Postscript figure provides a diagram of a firewall setup to better illustrate the relationships involved.