The Sages permitted asking a non-Jew to do a rabbinically forbidden melakha in cases of great need, such as to prevent suffering or loss, or for the sake of a mitzva. Such cases are a shvut di-shvut, a combination of two rabbinic prohibitions: the action itself is prohibited rabbinically, and the entire prohibition of asking a non-Jew to perform melakha on Shabbat is rabbinic.
In contrast, one may not ask a non-Jew to perform a melakha that is prohibited by Torah law, even for the sake of a mitzva. One may do so only for the sake of settling Eretz Yisrael or when there is a pressing communal mitzva need. Additionally, if not doing the melakha would entail suffering a very large financial loss, the Sages permit asking a non-Jew to do it. This is because they were afraid that if they were to prohibit this, the person suffering the loss would end up desecrating Shabbat himself on account of his great anguish. These laws are detailed above (9:11-12; 16:5 and n. 1).
Let us illustrate this principle. One may ask a non-Jew to remove a shofar from a tree in order to use it on Rosh Ha-shana, since the prohibition of using a tree is rabbinic (SA 307:7; above, 19:7). Similarly, one may ask a non-Jew to bring wine for kiddush or siddurim for the synagogue through a karmelit, as carrying there is rabbinically prohibited (MB 325:60; 21:3 above). One may also ask a non-Jew to bring food that is essential to the Shabbat meals through a karmelit, as this food enables people to fulfill the mitzva of oneg Shabbat. However, one may not ask a non-Jew to carry food that is not essential to the meal (MB 325:62).
One may ask a non-Jew to move muktzeh items in order to prevent a loss. For example, one may ask a non-Jew to collect scattered money, so that it will not get lost or stolen. One may also ask a non-Jew to bring bags of cement from the yard into the house so that they are not ruined in the rain (SA 307:19; see MB ad loc. 69).
If a door is squeaking so loudly that it is difficult to sleep, one may ask a non-Jew to oil the hinges. The relevant prohibition is rabbinic, since the door can be used even if the hinges are not oiled (Melakhim Omnayikh 6:1 and n. 1). Additionally, if mosquitoes are buzzing around in a room and disturbing one’s sleep, one may ask a non-Jew to spray and kill them. Since mosquitoes are not being killed to use their bodies, the prohibition is rabbinic (20:8 above).
If it is uncertain whether a particular action is prohibited rabbinically or by Torah law, one may ask a non-Jew to undertake the action for the sake of a mitzva or a great need. The prohibition of asking a non-Jew to perform melakha is rabbinic, and in general when there is uncertainty pertaining to a rabbinic rule, we are lenient.
If the lights went out in a synagogue or in a beit midrash, one may ask a non-Jew to turn them on using a shinui, since doing so is only prohibited rabbinically. If using a shinui is not feasible, as long as there is a pressing need that relates to a communal mitzva, one may ask a non-Jew to turn on the light even without a shinui. If possible, it is preferable to give him something to eat there, so that he will be turning the light on for himself, which is permissible even if it is not for the sake of a mitzva.