The hospital would convene a panel that includes maybe a high-risk-pregnancy expert, an ethicist, the neonatal ICU people, and they'd make a determination of what the options are.
If you can imagine a graph with the X-axis representing the weeks of pregnancy and the Y-axis representing mortality risk, with a red line representing the baby and the blue line the mother, you can visualize that as the weeks go on, the mother's risk line goes up and the baby's risk line goes down.
Wherever the red and the blue lines cross, that's where you do the C-section to try to save them both.
It's not a precise, mahematical-type determination. It's that, conceptually, everybdyu has to try to save both lives if they can. If they can only save one, they fully commit to saving the one (usually the mother.)
If it is IMPOSSIBLE to save the baby's life, --- well, nothing that is strictly impossible, can be morally obligatory.
But you still trat the baby respefully, as a dying baby. You deliver him whole, you trat him with whatever care you can offer, even if he only lives minutes. You don't go in and dismember him and haul him out in bloody chunks.