In order to answer your question, it is important to establish that discipline in the form of removing an unrepentant wrongdoer from the congregation is a form of judgement authorized by God to preserve the holiness of the congregation.
Examples are recorded throughout the scriptures from the time Gods people were organized into a congregation.
For instance, this direction was given to the nation of Israel in this regard:
Deut 13:6...
“If your brother, the son of your mother, or your son or your daughter or your cherished wife or your closest companion should try to entice you in secrecy, saying, ‘Let us go and serve other gods, gods that neither you nor your forefathers have known, from the gods of the peoples all around you, whether near you or those far away from you, from one end of the land to the other end of the land, you must not give in to him or listen to him, nor should you show pity or feel compassion or protect him; instead, you should kill him without fail. Your hand should be the first to come upon him to put him to death, and the hand of all the people afterward. And you must stone him to death, because he has sought to turn you away from Jehovah your God, who has brought you out of the land of Egypt, out of the house of slavery. Then all Israel will hear and become afraid, and they will never again do anything bad like this among you.”
This verse is significant in regards to your question, because despite God being the originator of marriage & family, when an individual rebelled against God, punishment was to be carried out despite family ties. This is not a contradiction of Gods command that “a man shall stick to his wife”, but rather shows how seriously God views rebellion against him. In Israel the punishment meant death, a permanent “cutting off”. Imagine how difficult it would have been to carry out Gods law. (Christians everywhere should be grateful we are not under the Mosiac Law). This verse also shows the discipline was to serve as a deterrent so others would not follow the same course.
Another reason a wrongdoer was to be removed from the congregation, was it impeded the flow of Gods Holy Spirit within the entire congregation. We can see this from another account recorded in the scriptures...
In Joshua 7: 1-26, One individual, Achan, secretly stole some items. Because he violated God’s explicit instructions, when Israel went to conquer the next city in Canaan, Jehovah withheld his blessing. When Joshua asked Jehovah why they had lost the battle, he was told “I will not be with you again unless you annihilate from your midst what was devoted to destruction”. Achan and his family (who were probably aware of his sin) were “cut off”—executed. Once that rebellious influence was removed from the congregation, God’s Holy Spirit flowed freely and Israel was successful again.
Regarding other serious sin (as when a death occurred) God commissioned Israelite elders to investigate. They were to establish facts, weigh carefully a manslayer’s motive, attitude, and previous conduct when deciding whether to show mercy. They had to determine whether the fugitive acted “out of hatred” and “with malicious intent.” (Numbers 35:20-24) If the testimony of witnesses was considered, at least two witnesses had to substantiate a charge of intentional murder. —Num. 35:30.
You can see from all these instances, the punishment of “cutting off” was for serious sins: apostasy, theft, murder. (Other sins that also required “cutting off” included disrespect of Jehovah, idolatry, child sacrifice, spiritism, desecration of sacred things, and practices as incest, bestiality, and sodomy.)
Today, Christians, while not under the Mosiac law, are also commanded to keep the Christian congregation clean, free from the influence of willful violators who deliberately “practice” sin. Some of the offenses that could merit disfellowshipping from the Christian congregation are fornication, adultery, homosexuality, greed, extortion, thievery, lying, drunkenness, reviling, spiritism, murder, idolatry, apostasy, and the causing of divisions in the congregation. (1Co 5:9-13; 6:9, 10; Tit 3:10, 11; Re 21:8)
This would not be a contradiction to “ not judge your brother”, rather it would be in harmony with the direction given by the apostles to preserve the holiness of the Christian congregation. In fact, the scriptures make clear while we are NOT to judge those outside the congregation (that responsibility belongs to God), older men were given the responsibility to judge willful violators within the congregation....
1 Cor 5:11-13. But now I am writing you to stop keeping company with anyone called a brother who is sexually immoral or a greedy person or an idolater or a reviler or a drunkard or an extortioner, not even eating with such a man. For what do I have to do with judging those outside? Do you not judge those inside, while God judges those outside? “Remove the wicked person from among yourselves.”
By not permissively overlooking willful sinfulness, a high standard of conduct befitting Gods holy people would (and should) be the norm in the congregation. Hypocrisy among those claiming to represent God was something Jesus fully condemned. This assures:
1) the free flow of Gods Holy Spirit in the congregation
2) others are not influenced by bad conduct
3) the congregation does not bring reproach upon God.
Thus, when someone visits a congregation of Jehovah’s witnesses (or one of Jehovah’s witnesses knocks on your door), you can be assured that person at least meets the Bible’s minimum code of conduct.
This all being said, disfellowshipping is a rare occurrence.
Firstly, because before one can be baptized as one of Jehovah’s Witnesses, an individual must have a good knowledge of the scriptures. They must fully understand the standard of conduct required to be in good standing in the congregation, be active in the door to door public ministry and attend all meetings. Obviously, someone who has reached that point in their determination to do Gods will is already dedicated, well before they present themselves for baptism. There’s no surprises and there’s no infant baptism. (Unlike in Israel, you were “born” in a covenant relationship with God; you didn’t have a choice)
Secondly, it’s really hard to be disfellowshipped. It’s a last resort when someone has changed their Christian course and just does not want to do what the Bible says. But the spirit behind disfellowshipping is never harsh. We are friends, families who have known each other, sometimes grown up together. The elders are our brothers, our friends and family as well. Members of the congregation truly mourn. When someone is disfellowshipped, they are encouraged to still attend meetings. They can sit with their families, go to the literature counter to request publications and can approach the elders. The elders also arrange to visit disfellowshipped ones to offer encouragement. In this way the congregation is obedient to scriptural counsel, but still keeps the door open for an individual who wants to turn their course around.
For further reading on this subject, please follow the links below.
https://wol.jw.org/en/wol/d/r1/lp-e/2015287
https://wol.jw.org/en/wol/d/r1/lp-e/1995003
https://wol.jw.org/en/wol/d/r1/lp-e/1102002039