I had a similar problem with my iPhone 4, out of the blue (pre-iOS 5 update). It was as if the network connection wouldn't "stick." Could select the network in the WiFi panel, showed all bars as a strong signal, but then when I'd try to actually go online, an error popped up saying I needed to connect to a WiFi network.
Uhhh...yeah....already did that.
I just noticed there was no wifi icon at the top of the screen either, even though there should have been. When returning to Settings > Wifi, it was turned on, but I to re-select the network again. And again. And again. No joy. Still wouldn't "stick."
This morning, I did this and it worked:
1. Settings > Wifi: Turn on like normal
2. Click blue arrow to right of network name
3. Click Renew Lease
That did the trick for me. The wifi icon is back at the top of the screen again, showing full strength. Weird that it occurred, but glad to be back in business again.