I rewatched the movies recently, and was wondering about the dormitories. Would the teachers have access if they needed to check on a student, or would they have to gain access via another student? E.g. If a professor was worried about a student, say they were depressed and they worried about their saftey, would the professor be able to go check on them?
The Gryffindor and Slytherin common rooms are protected by passwords. It seems that anyone with the passwords can get in, regardless of who they are. Even Sirius Black, a convicted mass-murderer on the run, was able to enter Gryffindor Tower simply by giving the password:
"Sir Cadogan, did you just let a man enter Gryffindor Tower?"
"Certainly, good lady!" cried Sir Cadogan.
There was a stunned silence, both inside and outside the common room.
"You — you did?" said Professor McGonagall. "But — but the password!"
"He had 'em!" said Sir Cadogan proudly. "Had the whole week's, my lady! Read 'em off a little piece of paper!"
Presumably, then, as long as a teacher had the password he/she would be able to enter those common rooms. We do in fact see Professor McGonagall entering the Gryffindor common room on several occasions, and on some of them she was clearly able to get in on her own. For instance, in Chapter Thirteen of Prisoner of Azkaban:
The Gryffindor party ended only when Professor McGonagall turned up in her tartan dressing gown and hair net at one in the morning, to insist that they all go to bed.
And shortly thereafter:
Professor McGonagall was back. She slammed the portrait behind her as she entered the common room and stared furiously around.
Perhaps the other professors also have the passwords to the Gryffindor and Slytherin common rooms, and thus could access them, but it is also possible that McGonagall had the passwords specifically because she was the head of house for Gryffindor (and/or because she was the deputy headmistress).
The Ravenclaw common room is accessed by answering a riddle. Presumably, anyone who can answer the riddle can gain entry to the common room. Indeed, in Deathly Hallows we see that McGonagall and Flitwick were able to gain access but Amycus and Alecto Carrow were not:
There was a rap on the common room door and every Ravenclaw froze. From the other side, Harry heard the soft, musical voice that issued from the eagle door knocker: “Where do Vanished objects go?”
“I dunno, do I? Shut it!” snarled an uncouth voice that Harry knew was that of the Carrow brother, Amycus. “Alecto? Alecto? Are you there? Have you got him? Open the door!”
The Ravenclaws were whispering amongst themselves, terrified. Then without warning, there came a series of loud bangs, as though somebody was firing a gun into the door.
“May I ask what you are doing, Professor Carrow?”
“Trying — to get — through this damned — door!” shouted Amycus.
“Go and get Flitwick! Get him to open it, now!”
“But isn't your sister in there” asked Professor McGonagall. “Didn't Professor Flitwick let her in earlier this evening, at your urgent request? Perhaps she could open the door for you? Then you needn't wake up half the castle.”
“She ain't answering, you old besom! You open it! Garn! Do it, now!”
“Certainly, if you wish it,” said Professor McGonagall, with awful coldness. There was a genteel tap of the knocker and the musical voice asked again,
“Where do Vanished objects go?”
“Into nonbeing, which is to say, everything,” replied Professor McGonagall.
“Nicely phrased,” replied the eagle door knocker, and the door swung open.
It is apparent from this that McGonagall was able to enter simply by answering the riddle, and Amycus was unable to enter even by force. It is also perhaps implied that Amycus assumed that Flitwick would have special access (as he was the head of Ravenclaw house).
Entrance into the Hufflepuff common room is never discussed in the books, but presumably it is not much different from the other three common rooms.1 That is to say, there is probably something you have to know, and anyone who knows it can gain access and anyone who doesn't know it cannot gain access.
The above is all in reference to the common rooms. However, the question here specifies the dormitories, which are accessed from within the common rooms. There doesn't seem to be any special security that keeps anyone out from a dormitory that is not there's. For instance, in Chapter Fourteen of Chamber of Secrets Ginny was able to gain entry to Harry's dormitory, and steal back Tom Riddles diary:
But his cheerful mood didn't last long. At the top of the stairs to the dormitory, he met Neville Longbottom, who was looking frantic.
"Harry — I don't know who did it — I just found —"
Watching Harry fearfully, Neville pushed open the door.
The contents of Harry's trunk had been thrown everywhere. His cloak lay ripped on the floor. The bedclothes had been pulled off his four-poster and the drawer had been pulled out of his bedside cabinet, the contents strewn over the mattress.
The only security we do know of between the common room and the dormitories is the slide that prevents boys from getting up to the girls dormitories:
Two fourth-year girls came zooming gleefully down the stone slide.
“Oooh, who tried to get upstairs?” they giggled happily, leaping to their feet and ogling Harry and Ron.
“Me,” said Ron, who was still rather disheveled. “I didn’t realize that would happen. It’s not fair!” he added to Harry, as the girls headed off for the portrait hole, still giggling madly. “Hermione’s allowed in our dormitory, how come we’re not allowed — ?”
“Well, it’s an old-fashioned rule,” said Hermione, who had just slid neatly onto a rug in front of them and was now getting to her feet, “but it says in Hogwarts, A History that the founders thought boys were less trustworthy than girls. Anyway, why were you trying to get in there?”
So at the very least, female professors would be able to enter all the dormitories, and male professors would be able to enter the boys dormitories.
This feature is only ever shown in Gryffindor Tower, so it may or may not exist for the other houses. The fact that Hermione quotes from Hogwarts a History that the founders thought boys were less trustworthy may indicate that this was a school-wide policy.
1. In external sources, JK Rowling claims that the Hufflepuff common rooom is accessed as follows:
The barrel two from the bottom, middle of the second row, will open if tapped in the rhythm of ‘Helga Hufflepuff’.
This also fits with the above — a teacher would merely have to know where to tap to be able to gain access.