Firewall script | NatScript | ReadMe
Mac OS X Unix base borrows a lot from FreeBSD, including the ipfw firewall. Apple has a GUI for activating and make some simple settings for it. If you want some more advanced settings you are free to do it the Unix way.
I have put up a example at Firewall script that is close to what I use myself. By default almost everything is closed in the example, to open it up simple uncomment the rules you need and reload it.
I used to maintain a Firewall and NAT startup script for Mac OS X 10.2 but I have no more use for it. I keep it here if someone is still interested.
If you want to build your own firewall the best option is to use OpenBSD. Packet Filter, PF is OpenBSD's system for filtering TCP/IP traffic and doing Network Address Translation. PF is also capable of normalizing and conditioning TCP/IP traffic and providing bandwidth control and packet prioritization.
Linux Firewall Projectfiles is a script for iptables in Linux. I use it for all my Debian GNU/Linux GNU/Linux servers. It's very easy to set up and from my testing it seems to work well.
For a more complex firewall on Linux Shorewall is very popular, it's also a script for iptables.
Or even better, get two, a hardware Firewall for your LAN and a software Firewall for each client.