There is halakhic discussion, starting from the Gemara, about the circumstances in which a talmid chakham may or must be excommunicated (publicly), and when it should be dealt with ‘behind closed doors’. It appears as a machloket between Resh Lakish and Rav Huna in Mo’ed Katan 17a (Resh Lakish’s opinion appears alone in Menachot 99b as well). Rav Huna maintains that an Av Beit Din who sins (‘she-sarach’) is not excommunicated, but given a ‘leave of absence’. If he repeats, then he is excommunicated, because of the chillul Hashem involved. Resh Lakish maintains that it must be ‘covered up like the night’.
Rambam (Talmud Torah 7:1) paskens like Resh Lakish that the matter should be dealt with privately. He actually divides it into two halachot: a truly prominent leader (chakham zaken, av beit din, nasi) is never excommunicated (rather, privately given lashes) unless he acts ‘like Yeravam ben Nevat’. When it comes to ‘any talmid chakham’, Rambam warns against jumping to conclusions and praises batei din which avoid excommunicating Torah scholars.
The Rosh discusses – and paskens – a story which appears earlier on the same page in Mo’ed Katan, in which Rav Yehuda excommunicated a Torah scholar because of ‘san’u shumaneih’. The Rosh gives a few examples of what this term includes: people who cause chillul Hashem, people about whom there are rumors which embarrass their colleagues, heretics, and people about whom it is said ‘May God forgive him’. From the Gemara it is clear that this scholar did not violate anything publicly. Rather, he sinned discreetly, but there were rumors about him (the sense of the Gemara is that the sins were sexual - AR). The Rosh does not address the machloket between Resh Lakish and Rav Huna.
The Tur, the son of the Rosh, and the Sulchan Arukh (YD 334:42) in his wake, synthesize the Rambam and Rosh. The Tur, like the Rosh, does not distinguish between a higher-level chakham and a general talmid chakham, but the Shulchan Arukh, following the Rambam, does. He repeats the Rambam’s formulation, and then adds the opinion of the Rosh as the conditions under which the general Talmid Chakham may be publicly excommunicated. The Sulchan Arukh’s conditions are very similar to those of the Rosh, namely, at least one of the following: occupation with heretical works, excessive drinking, colleagues are embarrassed by him, and he causes God’s name to be desecrated.