On "Positivism and the Separation of Law and Morals" (1958) and The Concept of Law (1961), ch. 5 and 6.
What's the relationship between law and morality? If law isn't founded on morality, what is it founded on? Hart was a leading figure in the philosophy of law, and wrote in the tradition of legal positivism that goes back to the British Utilitarians John Austin and before him Jeremy Bentham.
Legal positivism is a doctrine developed in opposition to natural law theory, which says that society's laws are in some way natural, that they express rights and justice as real, naturally occurring entities in the world (most likely in human nature), and that our obligation to follow law is the same as our obligation to follow other moral commands. So, for instance, a social contract theorist like Hobbes or Locke thought that societies naturally form due to the human condition, and because being in a society is better than being in a state of anarchy, we've implicitly made a commitment to obey the law which it would be immoral for us to break. Even back to Plato, we get the idea in the Crito that because society has given us so much, we owe it to society to obey the law: legal duty is part of morality.
A legal positivist, on the other hand, says that morality and legality are fundamentally different. Whatever one's views on morality are, and whether or not the formation of society and its laws is natural or inevitable, whatever system of laws a society devises is a contingent historical fact, and these laws may or may not be just. The fact that we can argue about whether a particular law is fair or just means that laws are not morally good just by definition, and so when judges are trying to determine how to decide some case according to the law, and thus need to figure out exactly what the law is, they should be looking at what the law says and at how judges have interpreted it in the past. They should not be looking at their own moral intuitions, on the assumption that the aim of the law is to be just and fair according to what an individual judge thinks that means. Specifically, a judge shouldn't look at a law and say, "That's unjust! Therefore it isn't really a law." It's perfectly coherent, Hart says, for a law to really be a law and yet be a terrible law.
This might seem obvious, but came up in practical terms, for instance, in considering whether law-abiding citizens under the Nazi regime could be convicted of crimes after the Nazis were defeated. If you want to punish someone who was "just following orders," then you either have to say that Nazi laws were illegitimate (not really laws), or that the post-Nazi government is allowed to retroactively call something illegal that was not in fact illegal under the Nazi regime. Hart argues that the latter strategy is the lesser of two evils, and that contorting the notion of law to implicitly include moral considerations legislators (even Nazi legislators) did not intend is unwarranted.
In each society, Hart argues, there's a foundation for law. In a simple one, the law might be just whatever the king or legislature determines it to be. In complex modern societies, there are multiple law-making entities, but also a system (the courts) that determine in case of conflicts what the law actually is. Since all laws as passed by legislatures are in general form, then it's the role of the judge to decide how such a general law applies to a specific case. If a law says "there shall be no vehicles in the park," that clearly rules out cars and means that walking is OK, but what about bikes, roller skates, mopeds, etc.? Hart calls these cases where it's not clear whether or not a law applies the penumbra. In such a case, a judge might have discretion to consider that the point of the law is and so how exactly it should apply. But this extrapolation, for Hart, should again not just be a matter of the judge consulting their own moral intuitions, but instead trying to figure out the "spirit of the law." Still, this "spirit" will in some cases be merely indeterminate, and so the judge just has to make a decision and is in that sense a legislator, acting to complete a law passed by the legislature.
Now, unavoidably, figuring out the "spirit of the law," especially in the cases where the law provides the least explicit guidance, inevitably involves figuring out what the law sensibly should be. So in that case, judges look to their moral intuitions. But that only works if the purposes behind those laws are good ones. A judge within the Nazi regime, trying to be faithful to its laws, will think not "is this the most humane way to interpret the law?" but "does this way of interpreting the law align with the purposes of the Fuhrer whose agents came up with it?" Does it effectively intimidate, oppress, etc.? This alone shows that morality and law are different, and so a judge doing a good professional job may be in fact furthering the ends of an evil law.
The purpose of Hart's book The Concept of Law is to explain what law actually is, and he tells the story about how laws historically emerge from customs. His predecessor John Austin had (about a century earlier) described law as just being the command of a sovereign: That law is analogous to someone holding you up at gunpoint and demanding your money. It's just that the government is always doing this to all of us, and so we don't consider it unjust and are in the habit of obeying it. But it's really the fact that authorities will enforce a law that makes it a law.
Hart disagrees, and distinguishes between primary rules that govern people's behavior directly from secondary rules that provide infrastructure to enforce primary rules. It's hard to see a law that specifies who gets to be a legislator or judge, and how long their terms will run and how much they'll get paid, as in any way like an order from a sovereign. Likewise, the laws that enable people to draw up wills and other types of contracts aren't well captured by Austin's theory.
A helpful secondary source some of us looked at is Raymond Wacks' Philosophy of Law: A Very Short Introduction. Read about the nature of law and legal positivism in the Stanford Encyclopedia. Watch Jeffrey Kaplan's YouTube lectures on Hart's book.