During childhood, children come into contact with a lot of friends. Some of these last for a lifetime (I still communicate with my sixth-grade best friend), and some are fleeting influences in your child's life. It is natural for parents to evaluate their children's companions, wanting to encourage friendships that elevate our children and discourage those which do not. By the age of twelve, a child's moral character is largely established, so after this age, it is unlikely that an influence of another will completely turn our child away from the teachings that we have worked to instill from infancy. This is not to say that your child is immune to peer influence. The areas that your child already has as potential character weaknesses can be brought to the surface by peers.
It is important for parents to know their child's friends and ask their child questions about their social circles frequently. Parents who exhibit genuine interest in the goings-on of their child's social circle regularly show that their life is important to them. Asking your child about each of their friends, their interests, activities that the pair or group do, etc. in a non-threatening fashion allows your child to open up if any concerns come their way.
There are several reasons your child may choose to become friends with another child or involve themselves in a clique of "bad" companions. They may need to "belong" to a group, and the group of "bad" kids accepts them. They may get a thrill out of being involved with a group of kids that are not part of the mainstream. Your child may feel a need to rebel and exert their uniqueness. (If an older sibling is part of one group, a younger sibling may join a different group to establish their own identity.)
As with any challenge, prevention is easier to work with than waiting until the issue becomes a real problem. While there are no guarantees, there are a few things you can try to prevent your child falling into a crowd of "bad" kids.
1. Make your house the "cool" house to hang out
Take the time to get to know your children's friends, and make your house a desirable one for gathering. Set up a spot for your child to be with friends, play games, listen to music and watch movies. (Some families get a pool table or ping pong table for their kids.) If your children's friends enjoy coming over, you will get to know them better, and your child is less likely to get into trouble at home than elsewhere. Supervise, but don't hover when your child's friends are over. Passing through the room from time to time gives you adequate opportunity for supervision.
2. Expose your child to a variety of friends
There is no shortage of opportunities for your child to engage with a variety of peers. Help your child select an after-school activity, club, etc. that appeals to their interests. They will have the opportunity to make friends with similar interests.
3. Don't be too heavy-handed
Parents that are strongly authoritarian can drive their children to rebel, and one of these ways is by deliberately choosing "bad" friends. If your discipline is fair and balanced, your child will have less reason to rebel against your wishes.