We see Filius Flitwick and others setting up protective Enchantments around Hogwarts before Voldemort comes. If this is so, how could Rubeus Hagrid enter the grounds with Grawp and Fang from the mountains?
He smashed through the boundary, using Grawp.
From the horse's mouth:
“Blimey, Harry,” panted Hagrid, “this is it, eh? Time ter fight?”
“Hagrid, where have you come from?”
“Heard You-Know-Who from up in our cave,” said Hagrid grimly. “Voice carried, didn’ it? Yeh got till midnight ter gimme Potter.’ Knew yeh mus’ be here, knew what mus’ be happenin’. Get down, Fang. So we come ter join in, me an’ Grawpy an’ Fang. Smashed our way through the boundary by the forest, Grawpy was carryin’ us, Fang an’ me. Told him ter let me down at the castle, so he shoved me through the window, bless him. Not exac’ly what I meant, bu’...”
(Deathly Hallows, Chapter 31, The Battle of Hogwarts).
It's unclear whether smashing through the "boundary" meant penetrating the protections that Flitwick and others had cast on the perimeter of the castle. It could just be that they broke through a wall and that there was no magical protection to get past.
After all, we don't really see much detail on the nature of the protections that the teachers used. Protego Horribilis is the only specific spell mentioned. The other spells are merely described as "incantations of great complexity".
“I can act from here,” said Flitwick, and although he could barely see out of it, he pointed his wand through the smashed window and started muttering incantations of great complexity. Harry heard a weird rushing noise, as though Flitwick had unleashed the power of the wind into the grounds.
(Deathly Hallows, Chapter 30, The Sacking of Severus Snape).
It would be reasonable to suppose that any magical protection deployed by Flitwick would be able to withstand the likes of Hagrid and Grawp. I think it's notable, therefore, that Hagrid says that he came in "by the forest". The teachers were most likely concentrating their defences around the parts of the parameter which were most easily accessible. The front entrance would have occupied most of their attention. They may well have assumed that the Death Eaters would not have considered trekking through the dense, dangerous and unfamiliar Forbidden Forest. Hagrid, Grawp and Fang, however, know the Forest like the back of their hands. They could negotiate the complex web of paths with ease.
What probably happened was that, in an attempt to avoid Voldemort and the Death Eaters by the front gates, Hagrid instructed Grawp to go round the side via the Forbidden Forest. Grawp simply smashed through the wall/barrier marker at the edge of the Forest, which had no magical protection.
This is a quote from Deathly Hallows - "The Battle of Hogwarts"
“And now — Piertotum Locomotor!” cried Professor McGonagall.
And all along the corridor the statues and suits of armor jumped down from their plinths, and from the echoing crashes from the floors above and below, Harry knew that their fellows throughout the castle had done the same.
“Hogwarts is threatened!” shouted Professor McGonagall. “Man the boundaries, protect us, do your duty to our school!”
Clattering and yelling, the horde of moving statues stampeded past Harry: some of them smaller, others larger, than life. There were animals too, and the clanking suits of armor brandished swords and spiked balls on chains.
So the defenders are given an explicit order to defend against attackers. I guess they are sentient and detect hostile intent. Or, it is also possible that enemy detection is based on trust, as per Fidelius Charm. In this case, trust is based on Professof McGonagall, who cast the spell.
Hagrid and Grawp came to help and could not be considered attackers in both cases.
I doubt they use the Dark Mark for enemy detection, as it is a mark of distinction; only Voldemort's inner circle have a privilege to bear it.
Unfortunately, the book does not give more details on the defenders' logic or way of thinking.