If it works with some WiFi networks the problem is in your router, not the phone. FIRST, go to the router manufacturer's web site and make sure you have the latest firmware revision. Older revisions of Linksys, Belkin and some other routers are not compatible with the iPhone.
See if you have MAC Filtering enabled on the router. This is a feature that prevents unauthorized computers from using the router. It is off by default, so you (or whoever set up your network) would have had to enable it. If it is enabled you must enter the MAC address of the iPhone into the Allowed list. You can find it by going to Settings/General/About and scrolling to WiFi address. At this point make sure there IS a WiFi address; if the field is blank see:
http://support.apple.com/kb/TS1559?viewlocale=en_US
If these are not problems connect to the WiFi network, then tap the arrow to the right of the network name and look at the entries. If IP address begins with
169.254 this is not a valid address; it means that the router is not honoring a request from the phone to assign an IP address. For most routers the first two numbers should be
192.168.
Next look at the DNS entries. Compare them to the entries on your computer. They should be the same. If there is more than one DNS entry try deleting the first one.
Let us know if any of this was useful.