Please note that this method is the only one which will allows you to keep your preferred networks saved.
All the other methods are bullshit.
Devices: iPhone 3G/3GS
OS: up to 3.1.3
Symptoms: no wifi network / unable to join any network
First step: Localization
You should proceed by connecting your computer to your iPhone via the netatalk protocol, which is the fastest and the easier way to modify files directly on your iPhone memory. Your wifi is not working, so you will have to connect your devices via bluetooth.
You can also use iFile (check cydia) or the Mobile Terminal, but it will be a little bit longer.
So, the first step is to locate the file "/Library/Preferences/SystemConfiguration/com.apple.wifi.plist" which, as its name stands for, is dedicated to wifi.
Second step: Modification
Most of the wifi issues are due to a couple of keys in the plist file which are set with the wrong parameter.
Let's modify it.
Locate the first one: <key>AllowEnable</key>
and set it to <true/>
Now locate the second one: <key>JoinMode</key>
and set it to <string>Automatic</string>
Save, and Reboot, it should work.
Last Step: it's still not working
Make a backup of your com.apple.wifi.plist file somewhere on your iPhone, and reset the network parameters on the Settings/General/(Last Tab), you will have to edit your new file, by copying the "List of known networks" hierarchy from the backed-up file to the new one, and you will not lose even one of them.