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.