Until Christ came along (Mt 19:12), and then Paul (1 Cor. 7:32-35), the only state of life known was marriage. The rabbis taught that a man was only half a man if he wasn't married by 20. But even in the Old Testament some of the prophets, like Elijah and Jeremiah, chose celibacy. By Jesus' time the great rabbis spoke of the possibility of "marrying Torah," that is, dedicating their whole life to the study of the Word. Paul was one who did, both before and after his conversion. Jesus is, of course, THE WORD, and infinitely more deserving of total dedication than the written word.
It should not be surprising, therefore, that Jesus should speak of some who would remain celibate ("eunuchs") for the sake of the Kingdom of God (Mt 19:12). St. Paul not only continued his pre-conversion celibacy as a Christian but recommended it for those who would be dedicated to serving God in this world (1 Cor. 7:7, 17, 32-35). He was speaking to a general audience and so he does not oblige it. But observe what he says in verse 17, "Only, everyone should live as the Lord has assigned, just as God called each one. I give this order in all the churches." This coincides with the admonition of Jesus to follow the vocation given by God, whether celibacy (Mt 19:12) or marriage (v.11).
Now Jesus said that if one could accept celibacy for the sake of the Kingdom one should and Paul says it is the best way for those dedicated to the Kingdom, and BOTH of them lived this total dedication to the will of the Father for the salvation of souls. Given the mentality of the world, both Jewish and Pagan, it was impossible at the beginning to find mature candidates for the priesthood who were not already married. Only with the arrival of a generally Christian milieu, where virginity and celibacy were honored into adult life, would celibate candidates for Holy Orders be generally available. Not surprisingly, then, the Church discerned with time that the gift of celibacy from God (who alone can give such a gift), together with the desire to serve God and His people, was an indication of a vocation to the priesthood. THIS has not always and everywhere been imposed in the Church, but almost immediately in Church history we find it recommended and even required in some places. Although Latin Rite Catholic priests for the last 1000 years have had to be celibate, Eastern Rite Catholic priests and priests of the Orthodox Churches (not in union with Rome) do not. However, ALL bishops in both the Catholic and Orthodox Churches MUST be celibates. They represent Christ to the people in their dioceses and Christ's bride is the Church (Eph 5:21-33) not someone else. So, it is entirely fitting that bishops not marry EVER and that priests not marry either, though in some traditions it is allowed PRIOR to ordination.
The fact that the apostles were married says nothing about the practice, since they were Jewish and followed the practices of their day, until Christ came along. Tradition affirms that they remained celibates after they followed Christ.
With the grace of Redemption came the possibility of celibacy and virginity for the sake of the Kingdom of God for more than the privileged few. It is a total gift of self to the Lord, fitting for those whose vocation is dedicated to love and service of God and neighbor. Unfortunately, it is something the worldly who do not understand the power of God have difficulty accepting, then as now.