Google will pay $391.5 million to settle allegations by 40 states that the search and advertising giant illegally tracked users' locations.