There's a GPS radio built into most modern smartphones. It is what the device uses to pin-point its own location, aided by a bunch of satellites that float above us, of course. This, however, isn't the only technique used to track the whereabouts of handsets and their users. Crowd-sourced Wi-Fi data also helps in the process. This is why if your phone's Wi-Fi is turned off, your mapping or navigation app might ask you to enable it for better accuracy.
But how does Wi-Fi know where we are anyway? Isn't its purpose to get us hooked to the internet? Well, while the latter is true, Wi-Fi can also be used to obtain a mobile device's position. That is possible thanks to the set of location services built into today's popular operating systems, namely Android, iOS, and Windows Phone. When enabled, these services scan the area for Wi-Fi networks and log their locations in a huge database. This database is shared across devices, which allows a smartphone or tablet to get its approximate location by knowing what Wi-Fi networks are around it.
Location pin-pointing using Wi-Fi data is less accurate than GPS, but it could improve location accuracy in cases when satellite signal isn't excellent or totally missing – inside of a shopping mall, for example. It is also what enables a Wi-Fi-only iPad, which lacks GPS hardware, to detect its approximate location.
If all of this is getting you worried about your privacy, there are ways of turning a smartphone's location services off. Just keep in mind that doing so would make your smartphone less smart than it is.