If I turn wi-fi on, the gps reading becomes very accurate. However, if it is off, the location has a error margin of even 2-3miles. How does having wi-fi help, even if I did not connect to any network? Do this networks broadcast their position, and where is stored the position for each router?
No, that's not how it works.
When you have an Internet connection, you can download DGPS data from the Internet. DGPS data provides corrections for satellite position errors, satellite clock errors, and variations in atmospheric propagation that reduce GPS accuracy. In the United States, these corrections come from the NDGPS initiatives. Other countries have similar projects.
The big companies all use a system of collecting WiFi APs with their positions.
I have an IPad without GPS and it still can tell me where I am most of the time. But in the center of my house (only my own AP visible) it used to think I was still at my old address.
So Yes, there is a database with the location of Wifi stations. Multiple databases actually.
And you have a pretty weak GPS signal.