The Sages offered a rationale for desecrating Shabbat to save someone’s life: “Desecrate one Shabbat so that he will observe many Shabbatot” (Yoma 85b). However, in practice, even when it is clear that the person being saved will not observe Shabbat, one is commanded to desecrate Shabbat to save him because the Torah strives to increase life. Therefore, we desecrate Shabbat to save a mentally impaired person (shoteh), who is exempt from observing the mitzvot. Similarly, we desecrate Shabbat for someone who is unconscious and about to die, in order to prolong his life for a short while (BHL 329:4, s.v. “ela”).
We desecrate Shabbat in order to save an unborn fetus, even if forty days have not yet passed since conception (Behag; Ritva; BHL 330:7, end of s.v. “o”). Similarly, we desecrate Shabbat to save a premature baby. Although in the past it was forbidden to desecrate Shabbat to save a baby born in the eighth month, whose nails and hair had not yet grown in, as it was certain that it would not survive, nowadays, with the improvement of medicine and the invention of the incubator, whenever doctors assess that the baby has a chance of long-term survival, we desecrate Shabbat to save him. (See SA 330:7-8; SSK 36:12 and n. 26.)
Technically, a Jew may not desecrate Shabbat to save a non-Jew, since one may only desecrate Shabbat for the sake of someone who is himself commanded to keep Shabbat. However, in practice, this rule only applies when another non-Jew is present to save his fellow non-Jew. If no other non-Jew is present, one must treat the non-Jew, even if this requires desecrating Shabbat. Since we want non-Jews to save Jews, we must save them as well. Thus, saving a non-Jew’s life is included in the category of piku’aĥ nefesh.