How was this rule enforced by the church. In popular media today priests aren't always depicted as the most pious men. And even in the middle ages themselves priests or monks aren't always depicted as such. Boccaccio's decameron depicts nun as not being as pious as they make themselves out to be. Also in the story of Reynard we see satirical stories of priests breaking their celibacy. So what were the reprecrussions of breaking celibacy. What did the church do to batle this?
This is one of those questions where the answer is "it depends". It depends on where, it depends on when, it depends on who. A note on who: priests and monks are very different types of religious, which means they were under different structures of authority. In the Middle Ages, priests ultimately, and with little question, were under the authority of their bishop, who was under the pope. Monks were under the authority of their abbot, who may have reported to the local bishop or to the pope, depending on the winds of politics.
The ideal of celibacy for priests and monks goes back to the very beginnings of Christianity, with the Pauline epistles advocating celibacy (in the eyes of the Catholic church for all clergy) and the canonical gospels which present a celibate Christ. However, how well celibacy was accepted and enforced varied a great deal across time. It was fairly acceptable, even into the early modern period, for both priests and even monks to have wives (also referred to as concubines). This was against canon law, but people just ignored it, and the church went through multiple waves of reform to stop it, starting in the eleventh century with Peter Damian (see his Liber Gomorrhianus).
Punishment for monks is explicitly laid out in the Rule of Benedict (here's a list of online resources for the Rule) which is the handbook for the Benedictine order, the dominant monastic order in Western Europe during the first part of the Middle Ages. The subsequent orders all had slightly different ways of dealing with disobedience. Chapters 23-25 lay out the course of punishment for somebody being totally obstinate. First the superiors deal with him privately, then he is publicly rebuked, and, if he still does not reform, he is excommunicated from the monastery. Chapter 44 explains how an excommunicant can return to the community. The private punishment would involve being counseled and made to do penance. Depending on how the monastery felt about sexual sin, how repentant he was, the specifics, etc. he would either be asked to do some kind of penance involving fasting and prayer, or he would be excommunicated right away.
Priests were more complicated. If they were rural with little episcopal oversight, they might break their vow forever with no problems. If there were a problem, they would be asked to put aside the wife and/or stop their sin. They might be given penance, they might be moved to a place where there was more oversight, they might be removed from the clergy, they might be excommunicated. The most effective punishment might have been to stop paying them.
In extreme circumstances, there would be more severe consequences. Some of the Knights Templar were executed for heretical acts which included sexual misconduct, for example, although historians (and many contemporaries) believe that the charges were fabricated.
What did the church do to combat this? Reforms. There were many waves of reform during the Middle Ages in which the church attempted to root out misconduct and enforce more stringent rules on people. As I mentioned, the first reform really concerned with sexual misconduct was in the eleventh century. Peter Damian and Pope's Gregory VI and Leo IX were involved in starting it, and it culminated with Gregory VII. Despite this, clerical marriage remained an issue through the Second Lateran Council and down to the Reformation.
Edit: In poking around, I found this article which points out something I forgot: priests were often fined for misconduct. The end has a nice table summarizing the author's research on fines in Spain. As the article emphasizes, this didn't really put a dent in priestly concubinage (she goes all the way down to the 14th century).