That's the mistake a lot of people make, they assume that if they put on their online masks they become anonymous, when in reality they just adopt a new identity. Sure they can adopt multiple ones, but they are just as traceable as any RL identity, in certain areas even more so. This illusion of anonymity is one of the things that lures people into a false sense of impunity. The other one is that online interactions are for the most part way less direct. But even with all of that, those people are socially held accountable for their actions. Any good community will shun those bad apples, any strictly moded environment is gonna ban them outright.
Of course those bad apples are gonna thrive in bad communities, but what's great about the Internet is that it provides you with way more freedom in choosing the environment in which you wanna spend time. I for the most part, only heard of those famous Internet assholes, but with the exception of League of Legends I never really come across em.
Instead, what Internet provides me with is the opportunity to talk to some of the smartest, nicest and/or otherwise most interesting people I know.It is also the venue for me to enjoy some of my more "childish" entertainment and unusual interests.
I'm not here to be an asshole nor are the people I associate with, I do not care if we are in the majority or minority here, either way avoiding people I want no business with is of no issue to me, because really, it ain't that hard.
As for why those people are as they are online, it's an endless and tricky subject to debate, the latest episode of MLP FiM tackled one aspect of it nicely. Being mistreated leads to mistreating others.