Although the eye is complicated, biologists still have a good understanding of how it evolved. In modern animals, they have found eyes of all different degrees of complexity. This gives them a good idea of the sequence of steps needed to go from a simple eye to a complex one.
In evolutionary theory, an inherited trait is favored by natural selection when it helps an individual to produce more offspring than its competitors. This allows the trait to spread widely.
In 2015 a team of biologists reported evidence that natural selection favors better eyes in a tiny crustacean called Daphnia. They captured these animals from ponds and found that their eyes varied in size. A bigger eye is a better eye, because it can gather more light and form a clearer image. To determine which Daphnia were producing more offspring, the researchers counted the number of eggs that females carried in their brood sacs. They found that animals with bigger eyes tended to produce more eggs.
Daphnia can reproduce both sexually and asexually. Scientists isolated individual Daphnia in the lab, so that they could only reproduce asexually. When they reproduce asexually, all of their offspring are genetically identical clones. By studying these clones, the scientists learned that genes had a dominant influence on eye size. Then good eyes can be favored by natural selection.
"Microevolutionary Evidence: The Eyes Have It" (University of South Carolina)
"Why the Eye? (Understanding Evolution)
"How the Eye Evolved" (The New York Academy of Sciences)