As a Catholic, this answer regarding Church discipline is an apostolic tradition. It can be shown from the earliest documents to be used. And as the Church evolved and matured, so did our understanding of church discipline. I’m going to focus primarily on excommunication which is the earliest form of church discipline.
Excommunication is the means of the Church to establish the loss of communion with what is called the Communion of Saints, and as a result, from the spiritual benefits shared by all those in union with Christ as his Church. This means that excommunication, and indeed all forms of church discipline, can only be conferred on the baptized. “For what have I to do with judging those outside? Is it not those who are inside that you are to judge? (1 Corinthians 5:12)”
There are of course other methods of Church discipline than excommunication, such as suspension for clerics, and interdict for clerics and the laity, irregularity ex delicto, and others. Excommunication is distinguished from these other forms of discipline because it is the removal of all rights of the Christian in the Church. The excommunicated do not cease to be Christians though, because the mark of their baptism is indelible. But they are considered exiled from the Church community. This exile is not indefinate and the Church desires it’s end as soon as the excommunicated individual repents of their serious sin.
As I said, excommunication was begun in the early Church, firstly by the apostles. In his first letter to the Corinthians, St. Paul rebukes the Church for allowing “a man living with his father’s wife (1 Corinthians 5:1)”. He tells them to excommunicate the man, “You are to hand this man over to Satan for the destruction of the flesh, so that his spirit may be saved in the day of the Lord. (1 Corinthians 5:5)” St. Paul also spoke against having the Eucharist with anyone who called himself ‘brother’ but who was “sexually immoral or greedy, or is an idolater, reviler, drunkard, or robber. (1 Corinthians 5:11)” Then he exhorts them to “‘Drive out the wicked person from among you.’ (1 Corinthians 5:13)”
But, St. Paul, in this proclamation of judgment also expressed hope that in the end “his spirit may be saved in the day of the Lord. (1 Corinthians 5:5)” St. Paul had a hope for later repentance and readmittance back into the community. He reaffirms this hope in 2 Thessalonians 3:15 and 2 Corinthians 2:5-11, but still sinners had to be removed as a wake up call for the seriousness of his sin.
After 217 AD, Pope Calixtus used the keys of the kingdom to loose this original discipline of excommunication for every mortal sin and he introduced the doctrine of indulgences, which we don’t have enough time to get into here, but “basically it was the sinless Body of Christ tolerating or ‘indulging’ members who were not yet saints and were struggling to overcome repeated sins.” And there developed the distinction between living in a state of grace verses a state of mortal sin for Christians within the community. 
Later, around the fourth century, serious sinners who were seeking forgiveness and reentry back into the community of the Church were assigned to the ordo paenitentium, or the order of penitents. At the beginning of Lent the ordo paenitentium were liturgically excommunicated from the Church and assigned to perform a penance which would last until Easter where the bishop would formally lift the excommunication, absolve them of their sins (by the authority of Christ) and grant them reentry into the Church. It was around the seventh century that confession evolved from the public proclamation of sins during the Mass to the singular one on one that we know of today. And the lenten ordo paenitentium fell out of practice while penance became very popular throughout the Church for all of her members.
Unfortunately, ecclesiastical authorities began to abuse excommunication and use it as a threat to extort money and information from parishioners. This abuse created a rift between the authorities of the Church and the laity. At the Council of Trent (1545-1563), all bishops and prelates were forced to use moderation in their use of discipline.
“Although the sword of excommunication is the very sinews of ecclesiastical discipline, and very salutary for keeping the people to the observance of their duty, yet it is to be used with sobriety and great circumspection; seeing that experience teaches that if it be wielded rashly or for slight causes, it is more despised than feared, and works more evil than good. Wherefore, such excommunications which are wont to be issued for the purpose of provoking a revelation, or on account of things lost or stolen, shall be issued by no one whomsoever but the bishop; and not then, except on account of some uncommon circumstance which moves the bishop thereunto, and after the matter has been by him diligently and very maturely weighed. (Sess. XXV, c. iii, De ref.).”
After this passage are other measures for the proper use of Church discipline upon her people. And, the Council of Trent was very effective in this matter. As the use of excommunication and other censures for means of coercing individuals has become delightfully rare. Because the Council issued a whole check and balance system, so that discipline might not be abused.
With all the disciplines of the Church, not just excommunication, but excommunication especially; the idea is to awaken the sinner to the reality of the consequences of their actions and the eternal punishment of their crimes. Excommunication and Church discipline is not meant to lock anyone out of the Church, but to bring them about to a conversion and repentance. This line of thought is seen in canon 2272 of the Catechism of the Catholic Church which speaks about the automatic excommunication for abortion.
“The Church does not thereby intend to restrict the scope of mercy. Rather, she makes clear the gravity of the crime committed, the irreparable harm done to the innocent who is put to death, as well as to the parents and the whole of society.”
“In all, while the Church imposes this severe penalty for just cause, she also remembers, ‘a broken and contrite heart, O God, you will not despise. (Psalm 51:17)’.” 
All scripture citations taken from the NRSVCE, the official liturgical scripture sanctioned by the Canadian Conference of Catholic Bishops (CCCB)
Addendum: In all things I submit to the authority of the Church if I have misrepresented any aspect of doctrine or history.