Neutrino oscillations with three flavors in matter: Applications to neutrinos traversing the Earth