Make sure wpa_supplicant is installed.
sudo apt-get update
sudo apt-get install wpasupplicant
Open the file /etc/wpa_supplicant/wpa_supplicant.conf so you can edit it.
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Unless you have a custom setup as far as your WiFI access point is concerned the contents of this file should generally look like this. Simply replace YOUR_SSID and YOUR_PASSWORD with the appropriate information pertaining to your access point.
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="YOUR_SSID"
psk="YOUR_PASSWORD"
proto=WPA
key_mgmt=WPA-PSK
pairwise=TKIP
auth_alg=OPEN
}
Then open the file /etc/network/interfaces.
sudo nano /etc/network/interfaces
Add the following if a wlan0 section does not exist or if it does it should look like so.
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Finally run the following command to bring the interface up.
sudo ifup wlan0
If you are using Raspbian Jessie and not the Lite version and can use the command startx it may be easier to install and run the wpagui package.
sudo apt-get install wpagui
Then run it. It is self expanitory how it works.
sudo wpa_gui