Start with a Raspberry Pi booted from a freshly downloaded Raspbian image (2015-02-16-raspbian-wheezy as of this writing). For wifi, I'm using an Edimax EW-7811Un Wifi USB adapter which was $10 on Amazon. I wrote this while running headless and connected via Ethernet, but the same commands will work when connected to a TV with a keyboard and mouse.
If configuring via SSH, try discovering the Raspberry Pi's IP address by running arp -a, though sometimes that table takes a while to populate. Otherwise just check the router's DHCP table.
Once you have a terminal on the Raspberry Pi, generate the necessary WPA2 pre-shared key with this WPA Supplicant command:
$ wpa_passphrase your_wifi_ssid your_wifi_password
That returns something like this:
network={
ssid="your_wifi_ssid"
#psk="your_wifi_password"
psk=bd71f30a8525904a582112795a099cf058ac6ded7d5599d3dde0076a2c8eb652
}
Backup the exising /etc/network/interfaces file then add the above psk hash to the wlan0 section so the file looks something like the example below. Don't forget to add dhcp to the wlan0 interface definition.
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid your_wifi_ssid
wpa-psk bd71f30a8525904a582112795a099cf058ac6ded7d5599d3dde0076a2c8eb652
Reset the network and check that it's working with these commands:
$ sudo ifdown wlan0
$ sudo ifup wlan0
$ ifconfig
After a successful connection, ifconfig should look something like this:
wlan0 Link encap:Ethernet HWaddr 74:da:22:3b:0f:56
inet addr:192.168.1.62 Bcast:192.168.1.255 Mask:255.255.255.0
The assigned IP address will likely persist, so make a note of it. Shutdown the Raspberry Pi with shutdown -hP now then disconnect power and Ethernet. Reconnect power to reboot and wifi should auto-connect! Enjoy your headless, Wifi Raspberry Pi!
My Raspberry Pi 2 was purchased at the best little DIY Electronics store in NYC's East Village, Tinkersphere.
Cross-posted to dotjoe.