The earliest known insects evolved in the Devonian, 420 million years ago, the same time that Earth was being colonized by plants.
It took another several million years for winged insects to emerge, such as the giant dragonflies that inhabited Carboniferous forests. Insects such as bees, beetles, and butterflies did not appear until later. Butterflies, along with moths, are part of the Lepidoptera order, and it wasn't until recently (in 2016) that it was possible to determine how ancient this group is.
Namely, a group of scientists engaged in excavations near an ancient lake in northern Germany found in sediments perfectly preserved flakes from the wings of a primitive moth that lived approximately 200 million years ago. The similarity of these flakes with those of today's moths has allowed scholars to assume that this ancient moth was equipped with a coiled proboscis (similar to the spirotromba possessed today by moths and butterflies) despite not being able to feed on nectar because plants with flowers had not yet evolved. Probably, the primitive moth fed on sap or other tree exudates.
The Natural History Museum in London holds incredibly rare and wonderfully preserved butterfly fossils, like the one pictured, from the Isle of Wight. This remnant still allows us to guess the color and striped pattern of this 34-million-year-old butterfly's wings. More than 100 other butterfly fossils have been described, and the oldest butterfly ever found has been dated to 53 million years ago, at the dawn of the Eocene.
Over millions of years, the number of butterfly and moth species has multiplied, several times, to reach the 160,000 species of Lepidoptera described to date. Of these "only" 19,000 are butterflies, but new species are being discovered frequently. One prediction is that the number of lepidopteran species will range from 250,000 to 500,000. If proven right, this estimate would make butterflies and moths one of the most species-rich animal orders on the planet.