Why do babies turn their heads from side to side rapidly when they are sleeping?

Dreams. This most likely represents the rem stage of sleep, when your baby is dreaming.
REM sleep. Most babies turn their heads side to side when they are going through different stages of sleep.