I've always found the idea of gangs goofy. A completely arbitrary, invented 'us vs them' construct. It's childish.
No, not really. There's an economy there that the gangs build around... Much like what the mafia does.
If you're going to be trafficking in illegal substances/activities, by nature you cannot rely on the police to enforce those who aggress against you. So you need to provide your own protection. And that protection has to be willing to retaliate against aggression outside the law--i.e. with violence.
So if drugs are illegal, but people still want them, there are going to be organizations built up to service that demand. Those organizations need to enforce loyalty and have their members prove loyalty--because if your personal "police" force isn't loyal to you, you have nothing. Like any business, you want to grow, and so do your competitors. But since it's an illegal product, you tend to grow by acquiring "turf", and shrink by losing it. And the only way to grow is to displace the rival gang on your border and take the turf away from them.
And like the mafia, gangs have their positive side. While it's a dangerous life, your gang will protect you and take care of you when you need it. While they may cause violence with rival gangs, they try to "make nice" with the residents/families in their own turf both to attract future members and to keep those families from "snitching" to the cops. And since the police often neglect those areas unless called, the gangs often provide the little security that one has in those neighborhoods.
It's not in any way an arbitrary 'us vs them' construct. It's a natural response to some very unnatural conditions.