Recently, I posted something about fetal heartbeats. In humans (and in mammals generally), oxygen and nutrients are transferred by the mother’s circulatory system to the placenta, and from there by the separate fetal circulatory system to where they are needed. As I noted in my earlier post, this process is functional in humans at about 21 days after conception.
In order for this process to work, the fetus obviously needs a beating, functional heart (although the heart continues to develop after it starts beating). It also requires a different kind of hemoglobin, which binds more tightly to oxygen than the mother’s hemoglobin does, thus facilitating oxygen transport across the placenta in one direction. Waste products, including carbon dioxide, are transported across the placenta in the other direction. The water-filled lungs, obviously, play no role in absorbing oxygen or getting rid of carbon dioxide.

Human fetal circulatory system, showing the ductus venosus and ductus arteriosus which partially divert blood away from the liver and the water-filled lungs (from American Heart Association)
There are alternatives to this placental system, however. Marsupials, such as kangaroos, do not have the same kind of placenta. Kangaroos are therefore not able to survive in the womb longer than about a month. Instead, they are born in a partially developed state, and crawl to the pouch, where they complete their development drinking milk and breathing air with their still-developing lungs.
Birds have yet another approach, developing inside an egg. Nutrients are packaged inside the egg along with the embryo. Oxygen and carbon dioxide diffuse in and out through the eggshell, and oxygen is absorbed by the embryo through the allantois. The allantois also acts as a dumping ground for nitrogenous waste. When the nutrients in the egg are exhausted, it is time for the bird to hatch.