The builtin firewall used by OS X is ipfw. To learn more about it from the Terminal you can enter
To create a new firewall rule to allow incoming traffic on to a particular port you can enter the rule from the Terminal as follows.
sudo ipfw add 40000 allow tcp from any to any dst-port portnum
portnum is the number of the port you wish opened. In this case I've added a rulenum of 40000 to the rule so that if you ever wanted to remove it the following command will suffice.
sudo ipfw delete 40000
If you enter the command
sudo ipfw list you will see the active commands. To reload the firewall issue the command
sudo ipfw flush
If you want to log the hits the rule is getting you can add the
log command as follows,
sudo ipfw add 40000 allow log ...
I chose the rulenum 40000 somewhat arbitrarily as rules exist from 1 to 65535, though certain numbers are privileged. This just makes it easier to delete the rule later, if needed.