People are fairly stupid. No matter how smart anyone is, they're stupid in some way.
What I try to do is point it out in an innocuous way. If they make a generalization, I point out that it might be a generalization and I'm not sure if that applies to everyone.
There's a certain degree of tolerance you need to have around people. While people may be stupid, the logical course is to try and educate them, or, at least, present your own opposing view in a respectful and courteous manner. This, for me, usually means asking questions.
Them: Women are so shallow. (Or: Men just want sex) Me: I don't know if all of them are shallow (or just want sex). You've never met any women (or men) who aren't?
As to why people generalize, it's probably a leftover survival instinct. It was probably advantageous to early humans who saw a person keel over after being bitten by a snake to say "All snakes are dangerous." It's not hard to see how that would get translated into modern terms. Several crimes were committed by people who had tattoos? "All people who have tattoos are dangerous."