The very start of mishnayos maseches Midos mentions that the kohanim guarded the bes hamikdash in three locations, including bes hamokad, a room on the north side of the mikdash courtyard. The mishna there says that if the captain of the guards caught guards asleep on the job, he'd hit him. The Rambam's commentary to the start of chapter 5 of mishnayos maseches Sh'kalim clearly says that the punishment applied to kohen guards. (L'viyim guarded the bes hamikdash also, and some explain the punishment in Midos as applying to only l'viyim.) However, it's clear from the start of mishnayos maseches Tamid that the kohanim slept in bes hamokad (and the context implies that this was while they were on guard). How do we reconcile the mishna that says kohanim slept in bes hamokad with the one that (according into Rambam) says that they were punished for sleeping on guard?
Given that there were different shifts of guards, it would be that they would sleep in the guard room while not on shift. During their shift, they had to stay awake and on guard.
At one point in Parashat Bamidbar, the Torah speaks of the tribe of Levi with the expression shomerei mishmeret ha-kodesh ("watchmen of the sacred guard" – 3:38) – which the Rambam understood as a reference to this tribe's responsibility to guard the Beit Ha-mikdash (Hilkhot Beit Ha-bechira 8:3).
Later in this chapter (8:6), the Rambam describes how the kohanim standing guard inside the Mikdash would sleep: "The kohanim who guarded would not sleep in the priestly garments; rather, they would fold them and place them near their heads, wear their personal clothing, and sleep on the floor – like all guards of royal courtyards, who do not sleep on beds." The straightforward implication of the Rambam's description is that the watchmen were allowed to sleep as they "stood guard." While at first it may appear difficult to understand how "watchmen" could fulfill their duties while sleeping, this becomes somewhat more tenable in light of the Rambam's comments in the beginning of this chapter. There he clarifies that the Temple did not require protection from thieves and the like, but rather as a display of honor to the holy site. Conceivably, then, since the guards were not necessary to protect the Mikdash, but rather as a means of showing honor, it was perhaps permissible for them to sleep during their shift.
Indeed, the Chelkat Yoav (2:43) understood the Rambam as allowing the watchmen to sleep in the Temple, and he enlists this ruling as a precedent to the possibility of performing a mitzva while sleeping. The context of his discussion is the question of whether circumcision may be performed on an infant while he is sleeping. One might have argued that since this mitzva is being performed on the child's behalf, some minimal level of involvement is required, if only that he be awake. The Chelkat Yoav demonstrates from the Rambam's ruling that Halakha recognizes the possibility of fulfilling a mitzva even during sleep, and hence an infant's slumber does not invalidate a circumcision.
Most other Acharonim, however, read the Rambam differently. After all, later in this same chapter in Hilkhot Beit Ha-bechira (8:10), the Rambam codifies the Mishna in Masekhet Midot (1:2) which tells that an official was appointed to oversee the watchmen and ensure that they were all awake. In fact, the official was authorized to strike a watchman found to be asleep during guard duty, or even burn his clothing. How can this ruling be reconciled with the Rambam's earlier comments, where he describes how the watchmen would sleep in the Beit Ha-mikdash?
For this reason, several Acharonim (including the Mishneh Le-melekh, the Ma'asai Le-melekh, and the work Be'er Sheva on Masekhet Tamid) explain that the Temple watchmen worked on a rotation system, whereby at every point during the night some kohanim stood guard while the others slept. When the Rambam describes how the watchmen slept, he refers to the "off-duty" watchmen, whereas later, when he tells of the official who made his rounds to ensure that all the watchmen were awake, he refers to the guards working their shifts, who were not permitted to sleep.