When Google Maps helps you avoid gridlock, you can thank the poor souls who are stuck in a traffic jam. Google measures and averages car speeds on the road by pulling data from smartphones that have the Maps app. It combines this info with historical route data to give you traffic information and travel times.
When you turn on your phone and allow "My Location" through Google Maps, you give its computers permission to record your location and speed. Combining this info with everyone else around you (and on your route) is the key to Google's real-time information. If you have privacy concerns, remember that the data is collected anonymously. No one can know where you are or give you a speeding ticket or based on your Google Maps data.
Real-time data can be good for updates on accidents and lane closures, but it's only half of the equation. Historical information is just as crucial. Google tracks and stores its users' travel times and uses that to update averages and estimates. This helps cancel out any data or vehicles that might skew real-time info, such as delivery trucks or taxis.
Google also weighs traffic that is closest to your current location, which makes sense since traffic patterns could change along your route or near your destination. It's certainly not a perfect science, but it's a vast improvement from hourly radio reports. Next time you're stuck in traffic, take solace in the fact that you might be helping another commuter avoid your fate.