Realising you cannot change them, only they can change themselves, I make sure not to let them drag me down with them. Also I spend the minimum time with them that I can get away with.
Also remembering not to get frustrated when you put forth effort in trying to help them. And don't expect a thank you or acknowledgement from them, even when you know that you have made a positive difference to them.
Try and keep things in perspective - is there anything positive about them. If you look really really hard with a microscope you'll probably find something. Nobody's all bad! Keep that in mind, and as stricktdating said, don't take it personally.
Lastly, be thankful that you yourself do not have their negative attributes and see what you could learn from their mistakes.