I had the same problem. My WiFi would be detected, it tried a lot of things, but still won't connect. This is what I did:
I connected the phone to iTunes and downloaded the latest iOS setup. This can be done by clicking the Restore buttun under the Summary tab.
Restore the OS through the phone once the download is complete.
It might so happen that the iOS on your device might have a corrupt file or something. Either ways, try it.
ALSO,
I accidentally keyed in the wrong passkey for my WiFi in the new iOS, the first time it was trying to connect. Maybe that could have worked and not this tiring process.
So, I urge you to first key in random letters instead of your actual passphrase and see what happens, if that doesn't work, try my tidious method.
Hope it helps, good luck!