Montague wasn't inside Hogwarts, nor was he outside it. He was ... elsewhere.
I think this question is partly to do with how Apparition works, partly to do with how the Vanishing Cabinets work and partly to do with the anti-Apparition charms that exist around Hogwarts.
As is well-established (at least for anyone who's read Hogwarts: A History), you can't ordinarily Apparate within the Hogwarts grounds. This charm did have loopholes, however. It didn't apply to house-elves, who could Apparate and Disapparate at will anywhere they wanted. And it was temporarily suspended on occasion to allow students to learn to Apparate.
"As you may know, it is usually impossible to Apparate or Disapparate within Hogwarts. The Headmaster has lifted this enchantment, purely within the Great Hall, for one hour, so as to enable you to practise. May I emphasise that you will not be able to Apparate outside the walls of this Hall, and that you would be unwise to try."
(Half-Blood Prince, Chapter 18, Birthday Surprises)
Nevertheless, we would expect the anti-Apparition charm to act against Montague if he tried to Apparate in from outside the Hogwarts grounds. Or indeed if he was moving from one part of Hogwarts to another.
As @Janus Bahs Jacquet points out in the comments above, however, it's doubtful that Montague ever truly left Hogwarts. The Vanishing Cabinet would ordinarily transport its inhabitant from one Cabinet to the other. Yet we know that the Hogwarts Cabinet was damaged from being slam dunked by Peeves in Chamber of Secrets. It's highly likely that Montague was stuck in a portal outside of space and time, never truly departing from Hogwarts and never truly arriving at Borgin and Burkes.
"The other's in Borgin and Burkes," said Malfoy, "and they make a kind of passage between them. Montague told me that when he was stuck in the Hogwarts one, he was trapped in limbo but sometimes he could hear what was going on in the school, and sometimes what was going on in the shop, as if the Cabinet was travelling between them, but he couldn't make anyone hear him..."
(Half-Blood Prince, Chapter 27, The Lightning-Struck Tower)
Because the Cabinet was broken Montague never left Hogwarts, so the anti-Apparition charm didn't apply to him. He wasn't Apparating in from outside. But neither was he moving from one area of Hogwarts to another. Rather, he was doing what the Anti-Apparition charm never foresaw anybody doing: Apparating into Hogwarts from outside time and space.
What seems clear from what happened is that, as far as the charm was concerned, Montague was neither Apparating or Disapparating within Hogwarts. He wasn't Disapparating because his starting point wasn't Hogwarts, it was the limbo between the Cabinets. And he wasn't Apparating because Apparating involves moving from one physical location to another. As such, Montague may have completely broken the mould and done what no other wizard had ever done before: Apparating into time and space from the outside.
So why did he end up in a toilet on the fourth floor?
We're probably pushing the boundaries of our magical knowledge here but I suspect that Montague ended up in the toilet because of his lack of proficiency in Apparition. Malfoy mentions that he hadn't passed his test and that, even though he made it back to Hogwarts, the attempt nearly killed him. Inexpert Apparition has pretty terrifying consequences.
"The Department of Magical Transportation had to fine a couple of people the other day for Apparating without a licence. It's not easy, Apparition, and when it's not done properly it can lead to nasty complications. This pair I'm talking about went and splinched themselves...They left half of themselves behind...So, of course, they were stuck. Couldn't move either way."
(Goblet of Fire, Chapter 6, The Portkey)
Apparating is difficult enough at the best of times. Montague was attempting something incredibly difficult and dangerous in trying to Apparate from the inter-Cabinet limbo back to Hogwarts. Montague probably tried to Apparate somewhere fairly familiar (like the Entrance Hall) but got pushed off-course because of the difficulty of what he was trying to do. Actually, he was pretty lucky that he didn't splinch himself. Especially as splinching sounds similar to the condition he was in before - stuck between two places without being able to move.
Finally, it goes without saying that his arrival in the toilet generates comic relief for the reader.