A firewall is a network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. Its primary purpose is to prevent unauthorized access to or from a private network while allowing authorized communication.