Contemporary liberals argue that it is not possible to legislate morality, giving both theoretical and practical reasons. First, virtue is a matter of free will, and therefore it cannot be coerced, so the attempt to coerce people into being virtuous is self-contradictory. Second, it is not possible to enforce legal restrictions, as the example of Prohibition demonstrates, and so attempts to enforce morality are unrealistic.
On the first point, it is absolutely correct that virtue cannot be “compelled.” It is not, strictly speaking, possible to legislate morality. Morality does not consist merely in certain actions, but in habits, dispositions of the will that incline us to act readily in a certain way. The coercion of law does not reach the soul, but only external behavior. Those moralists, therefore, who think that they can literally, by law, "impose their morality on others" are mistaken.
But the most thoughtful advocates of legal “regulation of morality” do not make such a claim. According to them, good laws do not impose morality as such, but rather help to create or foster conditions that facilitate the formation of good habits. Laws accomplish this goal in two ways. First, the law can, by its regulation of external behavior, reduce the frequency of certain acts, which affects habits. Second, law contributes to the formation of moral ideals. I will try to explain these effects in the following section, drawing especially on arguments made in Harry Clor’s classic book, Obscenity and Public Morality (University of Chicago, 1969)....
There is also a second way in which law can help to create or foster the conditions of morality. It can play a role in the formation of citizens' moral convictions.
To understand this, it is important to attend to what may be called a "sociology of moral convictions and beliefs." Moral ideals can and should be arrived at by the effort of human reason. That is ethics, a branch of philosophy. But human beings are not simply isolated and disembodied intellects who possess an innate knowledge of morality. They are social creatures who live in communities, and they are creatures born with potential abilities that need to be actualized or developed.
For reasons noted by Thomas Aquinas (when he explains why revelation includes some things knowable in principle by reason) and Alexis de Tocqueville (when he explains why dogma is inevitable), most human beings are not capable of producing their own comprehensive ethical theories. Indeed, no human being is capable of doing it, simply on his own. Not only are we not all Aristotles, but even Aristotle would not have been Aristotle without Plato, who would not have been Plato without Socrates. Moreover, even apart from individual variations in intellectual ability, many human beings simply do not have the time and leisure (in the original sense of the term) to develop their own moral theories. The result, as Tocqueville notes, is that human beings need dogma, authoritative guidance on the highest things, such as the relation of man to God and to his fellow men.
You can read the whole thing here.