During the Battle of Hogwarts, the Dementors fought for the Dark Lord. Why was this the case if the Dementors worked for the Ministry of Magic?
The Dementors had long since abandoned the Ministry.
The Dementors had already been (openly) loyal to Voldemort for two years by the time the Battle of Hogwarts took place. They joined Voldemort straight after his return had been publically acknowledged by the Ministry.
"It is great regret that I must confirm that the wizard styling himself Lord - well, you know who I mean - is alive and among us again," said Fudge, looking tired and flustered as he addressed reporters. "It is with almost equal regret that we must report the mass revolt of the Dementors of Azkaban, who have shown themselves adverse to continuing in the Ministry's employ. We believe the Dementors are currently taking direction from Lord - Thingy."
(Order of the Phoenix, Chapter 38, The Second War Begins).
Voldemort had planned this move since his return in Goblet of Fire.
...when Azkaban is broken open, the Lestranges will be honoured beyond their wildest dreams. The Dementors will join us...they are our natural allies...
(Goblet of Fire, Chapter 33, The Death Eaters).
Even Dumbledore foresaw that the Dementors had much more in common with the Death Eaters than with the Ministry.
"The first and most essential step is to remove Azkaban from the control of the Dementors -"
"Preposterous!" shouted Fudge again. "Remove the Dementors! I'd be kicked out of office for suggesting it! Half of us only feel safe in our beds at night because he know the Dementors are standing guard at Azkaban!"
"The rest of us sleep less soundly in our beds, Cornelius, knowing that you have put Lord Voldemort's most dangerous supporters in the care of creatures who will join him the instant he asks him!" said Dumbledore. "They will not remain loyal to you, Fudge! Voldemort can offer them much more scope for their powers and their pleasures than you can!"
(Goblet of Fire, Chapter 36, The Parting of the Ways).
Since Voldemort allowed the Dementors free reign to drain the happiness and hope out of everyone (Muggles and wizards, criminals and the innocent alike), the Dementors happily joined him the moment his return was openly declared by the Ministry.