Takfeer is commonly translated as ex-communication and means to declare someone a kafir or a fasiq. To give a summary on the most important points:
The Qur’an and Sunnah gives us clues about who is a Muslim and who is a kafir or a fasiq. It is not allowed to give judgement of takfeer without a valid evidence from the Qur’an according to the understanding of the salaf and the early scholars. This judgement is not to be taken lightly as it will be explained in the following. Therefore, it is only for those who have comprehensive knowledge of the matter of takfeer to give such a judgement.
Firstly, I want to mention that there is wisdom behind takfeer. It is a warning to those who believe or do things which make them a kafir or a fasiq and a way to isolate small groups or individuals with deviant believes or practices.
Secondly, declaring someone as a kafir or fasiq must be done with knowledge and wisdom because a false judgement includes telling lies against Allah and the one who is falsely declared as a kafir or fasiq. Furthermore, if the judgement is wrong, the one issuing the judgement is a kafir himself, as the Prophet (saws) said: “If a man declares his brother to be a kafir, it will apply to one of them.” (Sahihayn)
Thirdly, there are some conditions which include that the evidence from Qur’an and Sunnah must apply to the special deed that the person does and that there are no conditions preventing the person to be declared as a kafir or fasiq (such as insanity, ignorance, involuntary practice of deeds or utterance of words, misunderstanding, making a mistake in deriving a ruling, judgement of takfeer resulting in a greater evil).
There is something important to note: there are several ahadith saying that “the one who does such and such a deed is a kafir” or that “such and such an act is kufr”. Imam at-Tahawi explains in his book on Aqeedah that we do not declare a Muslim to be a kafir on the basis that he has committed a great sin unless he allows it. (§130 of Aqidah at-Tahawiyyah)
In the life of the Prophet, he has declared takfeer on some sahaba who were reluctant to fight on the battle fields. One of them was Ka’b ibn Malik who told his story where the benefit and wisdom of takfeer become apparent. (you can read it here)