This is a problem that we must all deal with, unfortunately. In my own experience, two things help:
- Always work for the smartest people that you can find.
- Don't forget that your job is just a job.
As to the first point, I have always found that the most intelligent people I've worked with were also generally the most fair. They weren't always easy to get along with, and they weren't always fair, but as a general rule, they were the people that were by far the best to work for/with.
As to the second point, it's not productive to single out problem members of your team and attack them. Be diplomatic. At some point you will likely need to be honest with others about the problems that you are having, but don't be eager to play the blame game. Take note of whatever concrete evidence you can gather about the problem individual's attitude or incompetence, and always be respectful when bringing it up. Never engage in speculation about what the problem person's issues are, and never rush to confront them about it. Simply do your best to be perfectly rational and objective, and don't complain about them any more than is necessary.
Both points also lead to a third point: never settle for a programming job that you don't love. If you're forced to work with problem team members and there are no super-smart people that you are learning from, then don't be afraid to shop around and find a new job. You will only limit your own potential to grow and become a great programmer if you stick it out at a poisonous workplace.