Mammals represent the latest evolutionary stage of one of the two major groups of amniotes (non-amphibian tetrapods), the synapsids. Non-mammalian synapsids, often called therapsids (although mammals are technically therapsids too) branched off from the other main group, the sauropsids (reptiles), in the Carboniferous period. The first known synapsid was Archaeothyris, a small lizard-like creature that lived 320 million years ago. Like the reptiles, synapsids evolved from Carboniferous amphibians.
The synapsids would go on to develop into the pelycosaurs, which would be the dominant terrestrial vertebrates for about 40 million years, until the middle of the Permian period, about 275 million years ago. Pelycosaurs are famous for being the first large (1 meter up to 3 meters or more) terrestrial amniotes. They had large tail sails, made from extended vertebral spikes, which are thought to have been used to regulate body temperature and for mating displays. Pelycosaurs, despite being synapsids, resembled reptiles superficially. This group was the most abundant land animal of the early Permian.
Most pelycosaurs went extinct or evolved into other groups by the end of the Permian. One group, the sphenacodontids, evolved into the next major synapsid group, the therapsids. Therapsids, once called "mammal-like reptiles," look like proto-mammals to the casual eye. This group evolved in the early Permian and stuck around for about 150 million years, into the early Cretaceous. This overlaps about 100 million years with the Age of the Dinosaurs. Before the dinosaurs emerged, during the late Permian, therapsids were the most numerous and successful terrestrial vertebrates. Unfortunately for the group, they were almost entirely wiped out during the Permian-Triassic extinction, 251 million years ago.
In a gradual process that took 70 million years, from the mid-Permian to the mid-Jurassic, one group of therapsids, the cynodonts, evolved into true mammals. Information about early mammals of the Mesozoic era is scarce, as most of these creatures were smaller than rats, and lived in contexts where fossilization was difficult. It is known that true mammals did emerge by 125 million years ago, approximately 40 million years after "mammaliaformes," mammal-like therapsids. One of the first mammals was Hadrocodium, although there is debate over whether this species was a true mammal. The first monotremes, placentals, and marsupials — the three major types of mammal — all appeared around this same time.
After the dinosaurs went extinct at the end of the Cretaceous period (65 million years ago) mammals rapidly diversified and took over the niches for large and medium-sized animals left vacant by extinct species. Today, mammals are the dominant terrestrial vertebrates, and reptiles scuttle underfoot. Of course, the reverse was the case throughout the Mesozoic era.