As a matter of fact, this arrangement still exists in some parts of Europe, only since there is no king of France anymore, it is the French president who selects the bishops, in Alsace-Lorraine (not in the rest of France, where there in separation of church and state–long story as to why the old treaty is still in force in Alsace-Lorraine). And recent presidents of France have not exactly been model Catholics (I believe Sarkozy is in his third marriage.)
Fortunately today, since the political power of bishops is not what it was, the government doesn’t care particularly much about who the bishop is, and they are more likely to listen to the Church. These treaties are called “concordats”, and if you do some research on that word you can find the whole history of what the different ones were, and which ones are still in force.
As for your larger question of why immoral, impious individuals could become bishops–you do not have to go so far back in history to find examples of bad clergy (look at the turmoil the Legion of Christ has just been put through). Unfortunately there is a long history of that in the Church; it is filled with sinners, though also filled with saints.
There was a heresy called Donatism that said that only the sacraments of virtuous clergy were valid; the Catholic Church rejected that heresy back in St. Augustine’s time, and ruled that the good or bad qualities of a priest make no difference as to the validity of them being priests or bishops.
Priests and bishops do have a much heavier moral responsibility on their shoulders, because of the great importance and holiness of what is entrusted to them. But its on their shoulders, and they remain individuals, some of whom are very pious, and some of whom are downright wicked. The wicked ones will face a harsher judgement, but God does not take away their freedom to chose good or evil just because they are clergy.
Anyway, I would recommend looking into concordats if you want to learn more about churrch-state relations, and looking into the Donatist controversy if you are more interested in the theological side.