How did animals evolve from laying eggs to giving live births?

The key to everything in evolution and biology is energy. Live birth probably is an adaptation to conserve energy in developing embryos.

The evolutionary adaptation of giving live birth has evolved separately hundreds of times, in several species of fish, reptiles, amphibians, arachnids, insects, and most notably in mammals. We call live bearing species viviparous (matrotrophic- ingests energy from the mother after fertilization) and egg laying species oviparous (lecithotrophic- all ingested energy solely provided by yolk). There is also an intermediate form we call ovoviviparity (lecithotrophic, with internal incubation); this is where the eggs are simply not laid until either right before or right after the offspring hatch. This is different from viviparity in that the mother has no additional energy input into the offspring after fertilization.

Viviparous species seem to evolve especially frequently in colder environments, where keeping your eggs warm allows them to spend more energy on development and less energy on thermoregulation. This makes your offspring more fit as they are born with a higher energy surplus than eggs incubated at cooler temperatures. Keeping eggs internally for even only part of the incubation is one way to ensure future offspring success. Another advantage to viviparity is that nest predation, an extremely common occurence, is made non-existent. One or both of these factors is likely the main driver of the evolution of viviparity.

To get more into the actual sequence of events, let's go through them with an imaginary species; imagine it as anything you want. Our example species begins as oviparous (lecithotrophic). The environment cools. Individuals who hold onto their eggs slightly longer due to natural variation have offspring which are slightly better off, as the offspring can spend more energy on growth. Over time the length of time that our species holds in it's eggs grows and grows. Eventually our species is laying eggs just before they hatch. But wait, now our species is making shells for it's eggs and that costs energy, why make the shells at all if the offspring are just going to hatch right away? Eventually a series of mutations turns off the shell making genes in an individual of our species and that saves energy. This trait is very helpful (it saves tons of energy and nutrients), and spreads through our population very quickly. We now have a ovoviviparous (lecithotrophic) species with no shell production. To get to true viviparity (matrotrophy), we need the mother giving extra nutrients to the offspring. This can happen in several ways. In most mammals several parts of the egg are highly modified to form a placenta, which feeds the offspring, but there are many other ways offspring can attain extra nutrients. In some species the offspring eat the mother's extra eggs, or each other, or secretions from the inside of the mother's "uterus", or literally bite into the uterine wall. Anything works here, the key is that providing your young with extra nutrients is important. Mammals evolved milk patches for feeding their young extra energy rich meals before they evolved away from egg laying (we can see milk patches on Monotremes).

/r/askscience Thread