Most modern GUI based distros handle setup and management of Wifi connections very well these days. However, sometimes you need to go the way of the command line. The following outlines the basics in Debian, plus some useful commands.
First, the Wifi device I had laying around was a Realtek based USB dongle similar to this. The driver for that is in the non-free repository, so I added the parts in bold to my /etc/apt/sources.list
deb http://ftp.ch.debian.org/debian/ wheezy main contrib non-free
deb http://ftp.ch.debian.org/debian/ wheezy-updates main contrib non-free
I could then install the driver:
apt-get install firmware-realtek
There are two config files to handle: The basic network configuration (/etc/network/interfaces), which also includes wired networks and the loopback, and the WPA wifi specific configuration (/etc/wpa_supplicant/wpa_supplicant.conf). Although it is also possible to specify wifi parameters in the network interfaces file, it is better handled by the wpa because then you can configure settings for multiple networks (e.g. home and work) as seen below.
/etc/network/interfaces contains the following:
# The loopback network interface
iface lo inet loopback
# Wired ethernet
iface eth0 inet dhcp
# The primary network interface
iface wlan0 inet dhcp
The loopback lo interface is configured, a wired eth0 port, and the wlan0 wifi. All networks are set to come up automatically, the last two use DHCP to get their address, and the Realtek nl80211 driver is specified as well as a reference to the WPA Supplicant config.
Here two networks are configured: A home network with WPA encryption and its passphrase, and an open network for work.
To bring the wifi network up, simply run the following. If iterating on the configuration, it's has to be stopped first.
ifdown wlan0 && ifup wlan0
Other useful commands while debugging this include:
For general network configuration and status:
For listing all available networks and their parameters. This works even before you have connected to a specific one, so it's a good test to see if the wifi device is even working:
iwlist wlan0 scan
For starting the wpa supplicant manually and checking the wifi configuration. Notice the specific driver and interface name:
wpa_supplicant -B -Dnl80211 -iwlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf