All perfect praise be to Allah, The Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad is His slave and Messenger.
The basic principle is that whoever hears evil, such as backbiting and the like, should denounce its speaker, and if the latter does not stop, then one must leave the place.
Al-Ghazaali said, “The listener is not free from the sin of backbiting unless he denies it with his tongue; if he fears to do so, then with his heart, and if he is able to leave (the place) or to stop the conversation, then he is obliged to do so...”
If a person is unable to leave, then he should not listen to the backbiting, and he should busy himself with the mention of Allah or do something else, such as put a headset over his ears and listen to something beneficial.
Imaam An-Nawawi said in his book Al-Athkaar:
“You should know that just as backbiting is forbidden to the person who does it, it is also forbidden for the listener to listen to it and approve of it. If a person hears someone who starts forbidden backbiting, then he is obliged to forbid him if he does not fear an apparent harm. If he fears such harm, then he is obliged to deny it in his heart, the way to do this is to mention Allah with his tongue and heart, or with his heart only, or he should think about something else so that he would busy himself from listening to the backbiting. If he does this, then he is not sinful for hearing it without listening to it in the above case…” [Abridged]
He can put his fingers in his ears, as it is confirmed in the Sunnah that it is permissible to put the fingers in one’s ears when hearing evil.
Naafi’ (the freed-slave of Ibn 'Umar) related that Ibn ‘Umar heard a flute and put his fingers in his ears and changed his course and then said to him, “O Naafi’, can you hear anything?” Naafi’ said, “No.” So he took his fingers out of his ears and said, “I was with the Prophet and he heard the same thing and he did the same.” This hadeeth was reported by Ahmad and Abu Daawood, and by lbn Hibbaan in his Saheeh, as well as by others. Al-Albaani classified it as saheeh (sound).
Allah knows best.