I see this fairly often.
It seems to happen when a 3G connection starts downloading data before the phone sets up the Wifi connection.
Wifi is dropped each time the phone sleeps for a while. As you tap the home button to wake up the phone, you can watch for the wifi icon to appear as you SLOOOOLY drag the "Slid to unlock" button.
But often, when In a hurry, I open the phone and start downloading mail or surfing (or whatever) before wifi is established, and when I do its a real fight to get it to cut over to wifi. Usually have to pop to airplane mode, killing what ever is running on edge/3G and then re-enable wifi, and it will usually connect.
Some things that make WIFI connections quicker, such as using only WPA-AES, 802.11G and low channel numbers on your router. No Dual mode encryption, no N, nothing fancy.
Some routers are slower than others. My Belkin absolutely *****, My Netgear is pretty fast at establishing a connection.
But there are times I just have to go in and power cycle the router to get it to connect again. It seems to get in a state where it will not allow a connect after a number of tries no matter what you do.
Please Apple: better messages than "could not connect". You insist on detailed problem reports when users say "didn't work", so turnabout is fair play.