There are a number of issues about the relationship between morality and law in a (pluralistic, secular) democracy like the United States. Among them are whether legislation should reflect moral principles, whether judges should interpret laws in light of moral values and principles, whether laws should enforce morality, whether laws are binding if they do not reflect moral principles, and whether it is moral or not to disobey bad laws, and what gives law its authority.
Sometimes morality is confused with religion and I have written about that elsewhere. But for the purposes of this essay, it will not matter whether someone's moral principles are based on religious doctrine or commands or not. The important traits will be the soundness, and perceived soundness, of any moral principles, not their genesis.
I am also not trying here to write a definitive work about all the issues involving the relationship between law and morality, nor to restate all the points others have already made about the issues I do address. Instead, I hope to simply shed some additional light on aspects of the relationship between law and morality in a pluralistic democratic country with a secular government.
Aspects of Law Not Based on Morality
First, as has been said by many people, some laws are managerial or administrative in that they institute behavior for procedural purposes that could equally well, from a moral or socially useful point of view, have been written in a different, or even opposite way. A common example is traffic rules about the side of the road on which one is to drive. Apart from consistency with bordering neighbors (especially for safety purposes of driving across borders and in neighboring countries), it does not matter from a moral standpoint which side a country (or a set of neighboring countries) adopts, as long as the choice is made among equally right (e.g., safe) options, though once a side is chosen by law, it is generally prudent, and morally obligatory without some good reason to the contrary to abide by the choice. These laws are not based on morality, in terms of their specifics. They are moral because they are a way of promoting social benefits of a certain kind in an optimal way.
Second, some laws are immoral, usually because they are unfair but sometimes because they are counterproductive or harmful; in some cases, egregious and reprehensible. Many laws about Jews in Nazi Germany and many laws concerning women and blacks in early U.S. law were morally wrong. Many apartheid laws in South Africa were morally wrong. But there have also been government programs set up by law that simply mistakenly harmed the people they were intended to help, such as aspects of the welfare rules that ended up trapping people in poverty rather than assisting them to escape it.
Laws, or a legal system with a lack of adequate laws, can also have wrong or immoral consequences even if the contents of particular laws are not unjust. For example, laws concerning evidence and procedure in courtrooms often lead to acquittals of obviously guilty defendants, and sometimes to convictions or continuing sentences and punishment of known or likely innocent ones. There is no reason to believe that just because a law passes, it is for the best or that it is right or moral, even if the people passing it think it is. If one were to be charitable about legislators, one might perhaps be able to argue that they pass those laws they believe to be right, whether those laws actually are right or not, but I think there is sufficient evidence legislators will often pass laws for political reasons -- to win or keep political support from those whom the law favors or to whom it panders -- even though they know the laws are bad or wrong. Either way, however, sometimes bad or immoral laws get passed that are perfectly legal.
Third, not all morality is enshrined in law because the law is in a sense 'incomplete'. Many unfair and wrong business practices are not anticipated and therefore not made illegal until someone invents and uses them in a way that clearly mistreats others. These practices are wrong and immoral from inception, but not illegal until the law 'catches up to them. In a sense, morality is 'complete' and applies to all acts, but the law typically is 'incomplete' and only applies to behaviors legislation has already addressed, or that the courts can interpret to have been addressed by implication in existing law. Law has to be 'invented' or manufactured; morality only has to be recognized. And in the creating of specific laws with specific wording, loopholes creep in because it is difficult to predetermine and specify those and only those acts intended to be covered. Morality does not have loopholes. It is probably impossible to make a complete set of laws that anticipate, enumerate, fully describe, and forbid every possible specific wrong behavior.
It might be possible to have general legal principles that distinguish all behaviors that should be legal from those that should not, in the same, or similar, way that correct general moral principles might distinguish between all morally right and all morally wrong acts, but it is not likely that either moral principles or legal principles can lead to a complete and specific (predetermined) enumeration of each and every right act in every circumstance.
Fourth, not all morality should be enshrined in law, because enforcing some morality would be far worse than not enforcing it. For example, even if it might be wrong for someone to lie in bed an extra half hour rather than having a good breakfast or getting to work on time, or even if is wrong for a child or husband to leave dirty clothes on the bed or floor, or even if it is wrong to break a prom date at the last minute for no good reason, those transgressions are not grounds for sending in the police. Liberty and autonomy are important values and they sometimes require letting someone make a mistake or do the wrong thing -- as long as the wrong that is done is not so bad or so costly that civil society has a legitimate interest to prevent it. There are sometimes disagreements about where the line should be drawn, but there are clearly some actions where autonomy is more important than being forced to do what is right by law, and there are clearly some actions where prevention of harm overrides autonomy and liberty. While society has a legitimate right to enforce morality in preventing great harm (or cost), it need not, and should not, make everyone do the right thing all the time. In cases where autonomy overrides prevention of wrongdoing, the law should not require people to do what they ought to. While it is wrong for people not to do what is right, it is worse or 'more wrong for the law to get involved in those situations where either autonomy and privacy are more important than enforcing the right behaviors, or in those cases where police or other government agencies will make things even worse by trying to enforce right behaviors.
There are some cases where even if a moral breach is bad for society, the social costs of trying to enforce morality in such cases would be worse than even the bad breach. Hence, martial law is not the sort of thing democratic societies generally tend to have, even if it would make streets safer; wiretaps are not permitted for just any speculative purpose; confessions cannot be coerced; and guilt must be proved by the prosecution beyond a reasonable doubt, even if guilty people sometimes go free because we have made the decision that it is better to free the guilty in cases difficult to prove reasonably than to risk convicting the innocent. Again, people disagree about whether the social costs of enforcement of some potential moral breaches are worth it or not, but a guiding principle seems to be that the law should not try to enforce moral principles where the enforcement efforts are (seriously) worse than the breach of principles would be, even when the original infraction itself is seriously bad.
Fifth, people disagree about moral issues. People also sometimes disagree about which laws should be created or kept, sometimes on moral grounds, sometimes on merely prudential or practical grounds where different consequences are predicted. When moral viewpoints conflict or are contradictory, law, unless it is to be contradictory itself, cannot reflect the morality of different people. Moreover in the United States, Constitutional rights will sometimes even prevent the law from conforming to the wishes of a simple (even substantial) majority of people or their representatives. How can laws conform to morality when people disagree about what is morally right or wrong, or when their collective wishes are 'thwarted' by the Constitution and by whatever minority is sufficient to prevent amending the Constitution?
This is sometimes stated as 'morality is subjective.' But disagreement can be about objective matters as well as subjective ones when it is difficult or impossible to know or conclusively demonstrate the correct answer. Disagreement, even unresolvable disagreement, does not necessarily make an issue subjective. I will say more about this later.
Sixth, a special case of the above is that it is often normal for people to believe that the status quo and traditional practices are what is morally right. It often is very difficult, especially for those who benefit from current practice, to notice or see there is something wrong with it, let alone agree with that assessment. Hence, they tend to see efforts for reform as unnecessary, destructive of a well-functioning system and social order, or even morally wrong. It is one view of the law that it tends to favor existing power structures and relationships, not necessarily or not only through some sort of Machiavellian attempt to maintain power through evil means, but because existing systems seem to work well (enough) and because they seem psychological to be normal and reasonable, especially to those in power.