The ductus arteriosus steals blood normally routed to the lungs and lets it flow straight into the aorta on to the rest of the body. The foramen ovale is actually a hole in the heart itself, allowing blood in the right side to flow through the wall into the left side and out, likewise, to the rest of the body. In both cases, the lungs are bypassed. Being born changes all of that. With delivery, the life line of the umbilical cord is severed.
Air hunger develops, and a reflex causes the newborn child to gasp for air.
Like in a good novel, the plot thickens. With this gasping, the lungs expand for the first time and convert from a crimped up, solid block of tissue to soft, air-filled bags. It's the change in the consistency of the lungs that starts all of the magic. When the lungs are in their unborn collapsed state, it takes a lot of pressure to try to pump blood through them. When they inflate at birth, this pressure falls so that it's easy for blood to flow into them.