That's all true, but misses the point. HR is presented to employees differently than those groups (e.g. they're the people you're supposed to go to for help when you see a wrong happen). In some cases, their actions may even make it seem like they're on your side, but they really aren't.
You're comparing individuals to departments. There are certainly people in HR who have put coworkers first. Depending on the company they may have succeeded or not in those actions. "The dictates of management" are what departments do.
This is the starry-eyed, optimistic view of workplaces.
In fact, in each case management works for themselves. HR, most of all. While engineering management has some pressure toward development goals, sales toward sales targets, and top management toward business development, HR has no such objective targets. Typically they even farm out recruiting.
What motivates HR management? In most companies, HR is the center of corruption. They have immense opportunities to collect kickbacks for everything they spend on. They have nothing in common with anybody else in the company, but everything with other HR managers: toilet plunger HR is the same as rocket fuel HR. The most popular discussion topic after hours, at HR conventions, is grifts.
This explains why, so often, recruiting is farmed out. You can't collect kickbacks from HR employees, but there are plenty to be had from recruiting agencies; likewise training programs, insurance policies, 401k accounts, and anything else they come in contact with. If they were not motivated by corruption, they would prefer a different department.
Management of other departments, and upper management, know this about them, and know that it doesn't matter how they feel about it. Companies have to have HR, and HR has to have HR managers. An HR manager not obviously on the take must be working a deeper grift that might well be worse than the obvious stuff. Extortion, for example, can be lucrative, and HR has access to lots of information others don't. It's better when they are taking care of themselves other ways.
I've only known one person personally who has ever done the cloak & dagger wearing of a wire to record a co-worker saying something that they wouldn't want the public or a judge to hear. She was the head of HR for a F500 company and did this to the COO. The buyout package for her to leave and agree not to disclose what she recorded was apparently quite substantial as she didn't have to work for years. Still a bit amazed that she both had to guts to do it and actually got away with it. HR is a completely different world.
My experience with HR is not that bad :-). Dealt with them in few occasions and I found them to be helpful, at least from where I was looking at the subject.
I can totally agree that they are not competent and not dedicated, as far as my personal experience goes. But the corruption I have not seen.
I really hope you have friends outside the company.
Your team is not automatically your friend. They are your coworkers (as is your boss and anyone else in the company). It's possible for friendships to grow there, but it's not common.
Legal is not your friend either. They work for the company, not for you.
Janitorial staff is not your friend either. They work for the company, not for you.
Sales is not your friend either. They work for the company, not for you.
Marketing is not your friend either. They work for the company, not for you.