Useful for when you're setting up a new wifi-enabled RPi without an external monitor/keyboard. The best way to do this is by using a TTY to Serial Cable, per the instructions here: http://learn.adafruit.com/adafruits-raspberry-pi-lesson-5-using-a-console-cable/
Note that this only works for non-hidden networks.
sudo nano /etc/network/interfaces
For regular networks, replace whatever's in there with this: