Hilchos Lashon Hara chapter 9, section 4

It is forbidden to live in the neighborhood of people who constantly speak lashon hara (baalei lashon hara), and even more to sit with them and listen to them even if one does not intend to believe what they say as we wrote earlier (6:2). If one’s disciple speaks lashon hara constantly one should banish him if one knows rebuke will not help.
If one finds oneself among a group of such people due to circumstances outside one’s control and finds they are speaking lashon hara, if one estimates that rebuke will stop them, one is certainly obligated by Torah law to rebuke them. Even if one estimates that rebuke will not stop them but figures it won’t make them speak more lashon hara, one is not allowed to keep silent. For people might say he is like them and agreed to what they said. One is also obligated to speak up and rebuke them for the honor of the innocent person they are speaking of. This is one of the reasons one is obligated to leave such a group, for one is punished if one listens and does not rebuke them.