A Synagogue/A Beis Midrash:
There is a difference in law between a Shul, which is the sanctuary of prayer, and a Beis Midrash, which is the learning hall. A sanctuary which also serves as a learning hall, as is common in most Shuls today, follows the same law as a learning hall.
Shul: A Synagogue is exempt from having a Mezuzah. If however it contains a room of dwelling [i.e. a room that a person uses to eat, sleep and the like] then it is obligated to have a Mezuzah. If the room of dwelling is not inside the main sanctuary, then the main sanctuary is exempt from a Mezuzah, although the main entrance which leads to the room of dwelling is obligated. [Thus those Shuls that contain other rooms besides for the main sanctuary, and include rooms of dwelling such as a hall for eating or living quarters, are obligated to have a Mezuzah on the main entrance, and on any other room of dwelling, however not on actual door of the main sanctuary, unless the sancturary leads to one of these rooms.]
Beis Midrash: A Beis Midrash is exempt from having a Mezuzah. However since there are opinions which obligate it to have a Mezuzah, it is therefore proper to place on its doors a Mezuzah without saying a blessing. If possible, one should place a Mezuzah with a blessing by a door that is obligated to have a Mezuzah and then immediately place a Mezuzah on the doors of the Beis Midrash in order so it be included in the blessing. If the Beis Midrash leads to a room of dwelling then it is obligated in a Mezuzah with a blessing, as is the law regarding a Shul.
One is to place a Mezuzah on the door of a Shul/Beis Midrash without a blessing. If the Shul contains rooms of dwelling, then all the doors that lead to these rooms are to have a Mezuzah placed on them with a blessing. If the Shul is not used for Torah learning [Beis Midrash], and does not lead to a room of dwelling, it does not require a Mezuzah.
 The reason: As any area which contains independent holiness, such as a Shul, is only obligated to have a Mezuzah if it is an actual place of dwelling. [Shach 286/2]
 Michaber ibid
 Rama ibid
 As the students learn inside it from morning to night it is similar to a dwelling place. [Shach 286/19]
 Michaber ibid; The Maharam Miratanburg placed a Mezuzah in his Beis Hamidrash to prevent evil spirits from bothering him. [Taz 286/9] [Seemingly this is the reason why today the custom is to place a Mezuzah in Shuls, as although there is no obligation, it nevertheless gives protection.]
 Shach 286/20