There are two types of Internet Protocol (IP) traffic: Transmission Control Protocol (TCP) & User Datagram Protocol (UDP). TCP is connection based, while UDP is connectionless. These protocols send information over the internet, and when they reach a computer, the protocol looks at the information and decides what to do with it. This is when the firewall filters the information packets that are at the right stage of the connection. Using something called stateful inspection, a firewall will determine if the information is appropriate for your computer or network to process.
For more info: firewall computer security