How do eyes work?
The ability to tell if it's light out or not has been around for about two billion years.
At that time, some eukaryote cells were able to sense changes in their surroundings and react to them. For instance, some cells had eye spots, which could sense sunlight. When they sensed sunlight, they sent energy to move the flagellum, which pushed the cell toward the light.
These eyespots continued into animals with more than one cell. Flatworms, which evolved around 550 million years ago, also have eyespots. But a flatworm doesn't waste energy building an eyespot in every cell. Instead, it has just two eyespots in its head. These eyespots, though, are already bilaterally symmetrical and are placed in about the same place as your eyes, and for the same reason - so that the flatworm can see where it is going before it gets there.
Sometime after that, some animals evolved eyespots that were sunken a little bit into the head, instead of level with the surface. Maybe this helped to protect the eyespots so they didn't get damaged. But it happens that sinking the eyespots into a hole also helps to focus the light, which let these animals see better than their ancestors. Once animals like roundworms began to use the hollow to focus the light, about 548 million years ago, they evolved ways to focus the light better - the holes became deeper, and the opening smaller, so they worked the same way a pinhole camera does.
Beginning around 542 million years ago, the arthropods (insects and crabs) had an early form of eyes. Over time, these and many other types of creatures evolved lenses and the muscles to move their eyes from side to side. Eyes come in a lot of different kinds, so they must have been evolved separately many different times in different ways.
Early mammals seem to have actually developed less good eyes than the reptiles had, because these mammals were awake mainly at night and slept during the day, as moles do now.
But after the dinosaurs died off, mammals began to take over daytime as well as nighttime, and different mammals developed different eyes according to what would be most useful for them. We evolved to have both of our eyes in the front of our face. We don't see as much on our sides or in back of us, but we can tell easily how far away something is, by using binocular vision. Some people think that human eyes may have evolved specially to let us see dangerous snakes, which evolved about the same time as humans did.