This is hard to prevent and even harder for parents to deal with. When children pick their friends, they choose people who they think are like them or are like they want to be. What if your child sees himself as a loser, as not belonging in the in-group? He will then choose his friends from the out-group, since his need to belong "drives" him toward whomever will accept him. These are youngsters who are having more than their share of problems and are more than likely getting into trouble. Your child may just hang around with anyone who will accept him or at least not reject him. The out-group is not choosy. They will let anyone hang around who goes along and does not act like he thinks he is better than they are. They are an easy home-base for children with low self-esteem and behavior problems.
Your telling him he cannot hang around with those children does little good. You likely cannot stop him; and if you do, he may then have no friends. Children you like and approve of will not let him into their group. This is a fact of life in the real world of children. He is only accepted by other youngsters with serious problems just when his need to belong is strongest.
Your child is in a nearly impossible bind. He wants friends, has to have friends; but his friends have more problems than he does. What can you or your child do? Along with getting professional help, you and he can talk honestly about the bind.
Try this approach. "Here is the problem. You're having some serious behavior problems. I know that and so do you. Now, your friends also have serious problems. You and I know that's true whether they know it or not. The problem is as I see it is you getting into trouble when you're around them. That's when you have most of your problems. Can you agree you get into trouble mostly when you are with your friends? If so, we have a place to start. I don't have any answers but think this is where to start looking for some. What ideas do you have?"
Understand there are no magic solutions or quick fixes. You and your child need to keep talking about it. Arguing about any of it is not going to help. If it could, the problem would be gone, for it is nearly certain your child and you have done enough arguing to fix a thousand problems if arguing were the answer.
A related circumstance is your child's being suspected of or becoming involved with a gang. This is a growing problem in all communities. It enhances your ability to talk knowledgeably with your child if you take a pro-active approach to learning as much as you can about gangs and gang-related behavior in your area. The police are usually your best resource. They can tell you about the extent of local gang activity, signs to watch for indicating your child may be associated with a particular gang, and specialized resources for children who exhibit signs of possible gang involvement. Be sure you do not consult with anyone who is not a locally recognized expert in gang-related behavior. It is a specialized area of practice in which most therapists are not experts. It also is useful to know most children who are involved with a gang do not deny their involvement, if asked. They may but normally do not. Ask, if you suspect involvement; but you also need to pursue your concerns, if your suspicions persist.