When a baby is in its mother's womb, how do the lungs breathe?

They don't. The lungs never inflate in the womb. They are at rest & compact like a dry sponge before it absorbs water. At birth the baby gasps forcefully, filling the lungs with air & it begins doing the work of gaining oxygen. The placenta, which did the work before can be clamped.
Placenta supplies O2. The lungs don't breath when the baby is in the womb. Oxygen is supplied to the baby through diffusion from the oxygen in the mother's blood to the fetal blood across the placenta. The oxygen then circulates in the fetal circulations. Carbon dioxide diffuses from the fetal circulation back to the mother's blood and the carbon dioxide is then exhaled via the mother's lungs.