A firewall is a network security device that monitors and controls incoming and outgoing network traffic based on predetermined security rules. Its primary function is to act as a barrier between a trusted internal network and untrusted external networks, such as the internet, by examining and filtering packets.