The Shulhan Aruch (Orah Haim 276) writes that it is permissible to ask a non-Jew to light a fire in a cold house on Shabbat, because people are at risk of falling sick as a result of exposure to the cold. It must be emphasized, however, that this ruling applies only in situations when the need arises, and was not intended as a matter of standard policy. The Shulhan Aruch was referring to a situation where the wood placed into the furnace or fireplace before Shabbat was consumed, and there was no longer any fire to keep the house warm. In such a situation, a non-Jew may be asked to add more wood. Today, however, we can simply set the thermostat or timer before Shabbat, and thus as a general rule, there is no reason to permit asking a non-Jew to turn on the heat during Shabbat. Of course, one may rely on the Shulhan Aruch’s ruling when the need arises, such as if there was an unexpected drop in temperature during Shabbat, if one forgot to turn on the heat before Shabbat, or some other mishap occurred that resulted in the heat being off. This is also permitted if there is an ill patient or elderly person suffering due to the cold conditions. As a matter of policy, however, one should not plan from the outset on relying on a non-Jew to turn on the heat during Shabbat. This applies both to homes and to synagogues.
There is a difference of opinion among the Halachic authorities as to whether the Shulhan Aruch’s ruling applies also to air conditioning. Although the Shulhan Aruch mentions only heat, some Poskim maintain that his ruling could be applied to permit asking a non-Jew to turn on the air conditioning under oppressively hot conditions. Tosafot (Medieval French and German Talmud commentators), citing the Yerushalmi, tell that the Roman emperor Antoninus once asked Rabbi Yehuda Ha’nasi for a blessing before embarking on a trip, and the Rabbi blessed him that Hashem should protect him from the heat. This indicates that heat can be harmful, just like the cold. Indeed, as opposed to cold weather, against which one can protect himself by wearing layers, there is no protection against the heat. Moreover, nowadays we are accustomed to air conditioning and are therefore more sensitive to heat, and in fact there are recorded cases of people dying as a result of heat exposure. Therefore, several Halachic authorities (including the Minhat Yishak and Be’er Moshe) allowed asking a non-Jew to turn on the air conditioning under very hot conditions on Shabbat. Other authorities (including Rav Moshe Feinstein, Hacham Bension Abba Shaul, and Rav Yosef Shalom Elyashiv) disagreed, and forbade asking a non-Jew to turn on the air conditioning on Shabbat. (Rav Elyashiv went so far as to say that this is forbidden even if people will be unable to learn Torah because of the hot conditions.)
Hacham David Yosef ruled that when the need arises, one may rely on the lenient position, and ask a non-Jew to turn on the air conditioning if the home or synagogue is very hot on Shabbat. He notes that since turning on air conditioning is likely forbidden only by force of Rabbinic enactment (as opposed to Torah law), there is greater room for leniency. Once again, however, this ruling should be followed only when the need arises, such as if the weather unexpectedly turned hot, or if one happened to forget to turn on the air conditioning before Shabbat, but not as standard policy. Moreover, this leniency applies only to turning on the air conditioner when the room would otherwise be oppressively hot, and not to adjusting thermostat by a degree or two to the ideal temperature. The basis of this leniency is that the Sages did not forbid asking a non-Jew to perform Melacha in a situation of Sa’ar (discomfort). Therefore, one may apply this leniency only if the conditions are truly uncomfortable. If the conditions are acceptable but not ideal, one may not ask a non-Jew to adjust the thermostat.
It should be noted that in general, non-Jews employed by synagogues should perform on Shabbat only those tasks which are permissible for Jews on Shabbat. Under very specific circumstances it may be permissible to ask a non-Jew to turn on lights, heat or air conditioning, but this should never become the standard procedure. These systems should be set on timers, and synagogues should rely on non-Jews only under unique circumstances when permitted by Halacha.
Summary: Heating and air conditioning systems should be set in homes and synagogues before Shabbat. If, however, the weather unexpectedly changed, or the system was not, for whatever reason, set before Shabbat, then a non-Jew may be asked to turn it on during Shabbat under very cold or very hot conditions. One may not ask a non-Jew to adjust the thermostat if the conditions are acceptable, even if the temperature is not ideal.