Google have a huge database of the approximate locations of a large number of phone network cell towers and wifi access points all around the world. If your phone can "see" one (or more) known cell-tower IDs or Wifi AP SSIDs, then it can query Google's database to ask where that is and use that for your location. The cell network towers have a large range (kilometers in some cases) so that alone won't give a precise location. As Wifi is a very short range signal your phone can locate you very precisely using that, particularly if you're also picking up Wifi signals of different strengths from neighbouring houses to triangulate from.
All of the quoted sections below are from: Google Maps Help - Location-based services
Because GPS is not always available and locations derived from cell
towers aren’t very accurate, Google (and other Internet companies) use
publicly broadcast Wi-Fi data from wireless access points to improve
our location-based services. By using signals from these access
points, your mobile device is able to fix its general location quickly
without using too much power.
Our Location Server uses only publicly broadcast Wi-Fi information to estimate the location of a device:
The MAC (Media Access Control) address. This is a number that identifies computing devices – whether PCs, laptops, tablets, mobile phones, or wireless access points – interacting with a network. MAC addresses usually look like this: 00:01:02:0A:0B:0C. The number is usually fixed for the life of the device. A MAC address tells you nothing about the owner or user of the equipment concerned. It’s just a string of characters that’s technically necessary for web pages and other content to be properly delivered to your device over the Internet.
This data has been collected in two main ways: as their Google Street View car drives around it always has a precise GPS fix, at the same time it listens out for Wifi APs that broadcast their identity and records where it was when it picked them up, and does the same for phone network cell towers; in addition they "crowd-source" the data from Google Maps users on mobile devices, as the Google Maps users travel around with GPS enabled Google Maps will check which cell-towers they're connected to and which wifi points they can "see" and periodically upload this information.
Google Maps on an Android handset is an example of a reliable channel, since we can be confident that the SSIDs and MAC addresses it sends as part of a location request haven’t been tampered with before being sent.
So, for example, one way to ensure that your changed SSID is submitted to Google quickly, open Google Maps on an Android Device with WiFi enabled, and use the My Location feature to establish a location fix in the vicinity of your WiFi access point.
See also How does Android get the coarse location?