System preferences -> Network -> double click the Airport item.
In this window, select By default, join: Preferred Networks
Click the options button. In the dialog, make sure "automatically add new networks to the preferred networks list" is checked and set "if no preferred network is found..." to be either "keep looking for preferred networks" or "automatically join an open network."