Laws and morals are both normative claims about the actions of individuals, but while morals are concerned with individual actions, laws must be concerned about societal-level ramifications of not just the actions, but also the enforcement of law. For example, most people would agree that adultry is bad and immoral. But as a society we've decided that its immorality isn't sufficient to overcome the ills of being punishable by the state, and thus it ought not to be illegal. Stealing to feed your starving family is moral, but we don't want to support theft in general so its illegal.
In a free society, one must recognize that legality ought loosely follow morality, with the ideal being that illegal actions are the strict subset of immoral actions which cause greater ill than the enforcement of their illegality. In the absence of an oracle into objective good and evil, a society must err on the side of not making things illegal which could potentially either be not worse than the ill of their enforcement, or not be immoral at all.
The all-to-common trap is to confuse the direction of influence: morality should influence legality, never the other way around. Allowing legality to influence one's perception of morality is simply surrendering your autonomy to those who greatest influence the law, and is a sure-fire path to authoritarianism.