How do eye doctors know if a baby needs glasses or not?

Examination. The baby's vision can be estimated as a starting point, based on looking at moving objects or preferring to look at an interesting thing over an uninteresting one. A special instrument called a retinoscope is shined ino the baby's eyes and "swept" back and forth by the ophthalmologist, and the way the light bounces back shows the necessary glasses prescription.
Testing. Most babies with vision issues of which the most common is an eye that turns inwards, will be brought in by their parents for evaluation. If the baby is very farsighted, misalignment of the eyes is frequent. If the baby is very, very nearsighted, generally decent vision will develop in both eyes. There are tests an ophthalmologist can do which will objectively measure eyeglass needs.