Data is crowdsourced and contributed by users. On a much larger scale, Apple and Google collect data from iOS and Android devices to power their WiFi/mobile tower based geolocation services.
Mozilla also has such a service although I believe they're deprecating it. You can still query it today, but as a privacy measure you need to include the MAC of 3+ SSIDs to get location info, since otherwise you could check the SSIDs from probe scans of passerby to locate their homes.
Android's Location Services: https://support.google.com/android/answer/3467281#location_a...
iOS's Location Services: https://support.apple.com/en-gb/102515
In fact, Google provides it as a paid API: https://developers.google.com/maps/documentation/geolocation..., but you require BSSID's and not just SSID names to try to curb abuse.