Native iPhone apps use CoreLocation, which will take advantage of GPS, cell tower triangulation or Apple's/Skyhook's database of WiFi access point locations to detect the user's exaction location. (Incidentally, native Mac apps on Mac OS X 10.6 and higher also have access to CoreLocation.)
Web sites that use the W3C Geolocation API can also request your location -- on Mobile Safari this uses CoreLocation under the hood, but it can be implemented by different browsers in different ways. As Igor points out, Firefox 3.5+ can respond to Geolocation requests (they use WiFi network IDs that they send to Google).
The W3C Geolocation API is still in draft form, but the Javascript is very simple to use. Read the samples on the spec page, or you can look at my test site (which also explains IP geolocation -- another technique that isn't quite as precise).