In the United States, under what circumstances is ignorance of a law or legal principle a defense against legal action?
In general, ignorance of a law is an excuse only when the law specifically says it is.
There are extreme cases where a law has been found invalid because it did not give people enough notice. Actually, only one case I know of -- Lambert v. California, 355 U.S. 225 (1957). The city of Los Angeles passed a law making it illegal for anyone who had ever been convicted of a felony to remain in the city for five days without registering as a felon. Lambert was arrested on suspicion of doing something else, but convicted for being an unregistered felon. The Supreme Court held that it was a violation of Lambert's due process rights to convict her of a crime she had no way of knowing about, or even suspecting. This is a very rare, extreme case, and does not apply to things where people might be expected to suspect that there might be a law.
Otherwise, notice of a law is only required if the statute says so; for example, if a speed limit law requires the posting of signs, but the signs aren't posted, the law may not be in effect. However, ignorance still isn't an excuse--if the sign is posted, but you just didn't notice it, you're still on the hook.
The legal principle that a person who is unaware of a law may not escape liability for violating that law merely because he or she was unaware of its content is referred to as Ignorantia juris non excusat or ignorantia legis neminem excusat, and there is a nice Wikipedia page about it, which includes exceptions such as:
There were, particularly in the days before satellite communication and cellular phones, persons who could genuinely be ignorant of the law due to distance or isolation. For example, in a case in British Columbia, a pair of hunters were acquitted of game offenses where the law was changed during the period they were in the wilderness hunting . In reaching this decision, the court refused to follow an early English law case in which a seaman on a clipper before the invention of radio was convicted even though the law had been changed while he was at sea (Bailey (1800) Russ & Ry 1).
Another case: Cheek v. United States
Cheek v. United States, 498 U.S. 192 (1991), was a United States Supreme Court case in which the Court reversed the conviction of John L. Cheek, a tax protester, for willful failure to file tax returns and tax evasion. The Court held that an actual good-faith belief that one is not violating the tax law, based on a misunderstanding caused by the complexity of the tax law, negates willfulness, even if that belief is irrational or unreasonable. The Court also ruled that an actual belief that the tax law is invalid or unconstitutional is not a good faith belief based on a misunderstanding caused by the complexity of the tax law, and is not a defense.
In many jurisdictions, the intent element of crimes such as trespassing, burglary, and theft can be overcome by a good faith, but mistaken, belief that the real property upon which the trespass or burglary took place, or the personal property that was stolen, was actually the property of the person who entered onto the real property or took the personal property.
For example, suppose that you accidentally take a briefcase that looks like yours from an office, but this is really someone else's briefcase full of $100,000 in cash. You have not committed theft, even though the question of whether or not you owned the briefcase that you took is a matter of law for many purposes.
In a more legal and less factual example, suppose that you are the lender on a car loan and the relevant statute gives you the right to repossess the vehicle upon default only if you send a notice to the car owner in the mail. You weren't aware of the requirement to mail a notice and so you didn't send one. When you repossess the car due to a default on the car loan, you have wrongfully repossessed the car which may give rise to civil liability, but your good faith belief that you had a right to repossess the car probably prevents you from forming the intent necessary for the repossession to constitute criminal theft. So, your ignorance of repossession law may be a defense to the crime of theft in this case. If you knew you had to give notice to repossess the car and when ahead and repossessed it anyway without doing so, you would probably have committed the crime of theft.
Similarly, if the crime of poaching consists of knowingly hunting an animal on public property, and you believed in good faith because you suck at orienting a compass, that you were on your own property, that might be a defense to that crime, even though the status of the property as public or private is in some sense a question of law. If the same law were written as a strict liability offense, however, ignorance of the fact that you were on public property would not be a defense.
I think there may be indirect situations. Let's say you are asked in court whether you have ever committed the crime of theft. To give the correct answer, you need to know the laws about theft. You might have done something that was theft, but because of your incorrect knowledge of law you believe it is no theft, so you answer that question in court with "no". As far as lying in court might be a crime, ignorance of the laws of theft would be an excuse.