Shemiras Halashon Halachos
Hilchos Lashon Hara chapter 6, section 5
We wrote in paragraph 2 that even listening to lashon hara is a Torah prohibition. This is if one went to listen. But if one was with a group of people who began speaking lashon hara and one reckons that rebuking them will not help at all, it depends. If one can leave or stop one's ears with one's fingers it is a great mitzvah to do so as the sages say (Ketubos 5). But if one cannot leave and reckons that stopping one's ears is also very difficult as the people will mock him and he will certainly not do this, he should at least strive not to believe the lashon hara.
This requires three conditions. a) He should firmly decide that he does not believe the denigration. b) He should not enjoy from the lashon hara. c) He should not make any movement that the speakers can interpret as agreement to the lashon hara but sit like a stone. If he can maintain an angry expression that shows he does not agree to their lashon hara it is even better.
Hilchos Lashon Hara chapter 6, section 6
This applies if they were not talking lashon hara when he first sat with them. But if they were already speaking lashon hara or if he could leave and does not bother to do so, or if he knew these people always talk lashon hara, he is considered a sinner as they are even if he doesn't not like what they are saying and does nothing to encourage them for he transgressed the sages injunction to keep far away from unsuitable speech.
If he has intent to enjoy their words his sin is too heavy to bear and he is inscribed above in the book of remembrances as a wicked person and a baal lashon hara. For we find in Pirkei d'Rabbi Eliezer that Rabbi Eliezer the Great ordered his son Hurkenos as follows: My son, do not sit with a group that speaks evil of their fellow men for when the words rise above they are written in a book and all present there are written beneath the name of an evil group and baalei lashon hara. Therefore one must keep far away from such people.