First, Wifi goes off anytime your phone is asleep. When you wake the phone, even before you slide the un-lock slider, you can watch for the appearance of the Wifi icon. It can take anywhere from 1 to 10 seconds, with three being the average. IF it takes longer, it will probably silently fail.
If you start a task that requires a network connection before wifi appears, then wifi will not work till you stop all network traffic.
If the Edge radio fires up and starts talking before the wifi gets going, it will block the wifi from working. (I believe this is a software bug, its easily demonstrable starting a long-mail download on Edge, an then turning on Wifi. It will fail till edge stops transmitting. Hold the phone near computer speakers so you can hear the Edge chatter in the speakers.)
There have been several reports that the high-numbered channels on a wifi router seem to cause problems for the iPhone. If you control the router, lock it down to a low numbered channel, like 1 thru 6. Lower is better as it gets you out of contention with other types of house phones, and it penetrates walls better.
If you own the router make sure you have a unique SSID (don't leave it saying Linksys). Also, don't use multi-mode if you can avoid it. The iPhone likes 180.11G, not A, or B, and not N.
For encryption WPA2-Personal (AES) is the most reliable and fastest negotiating encryption. (Its all done in hardware).
Its best to select a Fast starting Wifi protocol.