How does wifi determine a device's location? [duplicate]
Purely Wi-Fi-based geolocation requires an active Internet connection, and is done by doing a Wi-Fi scan, noting the BSSIDs (the unique numeric hardware MAC addresses) of the Wi-Fi APs (wireless routers) in range, and sending that list of BSSIDs to a web service that looks up the known geo-coordinates of those APs, and reports back what your geo-coordinates must be, based on what APs you're closest to.
The databases of what Wi-Fi APs are where are kept up to date by smartphones and 3G/4G tablets. Those devices have GPS receivers in them, so they know where they are by GPS. They periodically check their GPS location, and then do a Wi-Fi scan to see which APs are nearby. Then they report that information back to the vendor (Apple, Google, Microsoft, etc.) so that those vendors can keep their Wi-Fi geolocation databases up to date.
Devices that have GPS can't always get a good signal lock on enough GPS satellites to tell their position accurately (indoors, or in "urban canyons" among tall buildings, or in vehicles), so sometimes Wi-Fi-based geolocation can be used to assist GPS. Also, for devices that have WWAN radios (smartphones, 3G/4G tablets, etc.), location of cell towers can be used as well.
It's possible to do Wi-Fi geolocation without an Internet connection, but to do that, you'd have to cache a copy of the database, which might be too big. But a smartphone might have an optimization where it caches sections of the database for the city/area it already knows you're in, so that even if you don't have an Internet connection later in the same day in the same region, it can still look up your current position using the cached part of the database. If law enforcement gets ahold of your phone, they could possibly look at what areas your phone has cached as evidence of where your phone was on what date within the last few days.
Modern OSes use these methods together (GPS, cell tower, Wi-Fi) to get an idea of where your device is, and make that available to apps via a "Location Services" API. For websites, there's a web browser standard geolocation API. OSes and web browsers that offer these APIs usually ensure that the user must be prompted for permission before an app or website is allowed to use the API to determine where you are.
The least reliable method of geolocation is IP-address-based geolocation, or "GeoIP". That uses public records of what IP address ranges have been assigned to which regional ISPs, or which IP address ranges have been known to be deployed in various cities/regions by bigger ISPs. That's how sketchy websites offer you ads for meeting hot sexy singles in $YOURCITY tonight. Because any website (or web ad server) you connect to can see which IP address your HTTP request came from, they can use this to get an idea of what city you're probably in, without using the web browser geolocation API, thus without you being prompted to give permission.
They can use several methods, the most common on smartphones is cell towers.
http://www.antennasearch.com/ and http://opensignal.com/ are helpful in this but you will need to know data from the phone and or carrier to really play with these sites.
You can also use the GPS that is built into the smartphone but most phones allow the user to turn that feature on or off.
On a laptop you will most likely get location data based upon your IP address. A wifi hotspot may or may not be a known location but the gateway that you use to connect to the internet will under almost all circumstances be a known location. A fair amount of wardriving(no link as I am behind a corp proxy) has been done over the years and a lot of wifi hotspots, commercial and residential, have been mapped and that data is publicly available. I can't confirm but I would bet that Google Maps has a done a fair amount of this and may use the data to some extent.
When you say how does wifi determine the location, what is actually happening is the outward facing router the wifi is connected to will have a known location to the ISP and thus ads, local news, websites, can get this information and show targeted or location specific data to the user.
On a side note an IP address can reveal a fair amount about a person, but it must be a real, Internet routable IP, the IP address assigned to your router by your ISP will only point to your ISP's router in many cases. The IP that goes out from there can be used to at least narrow things down to your ISP and with some heavy traffic analysis the location may be able to be narrowed done but that is hit or miss and it deep magic in most cases.