One of the more interesting, and confusing, aspects of the criminal law is the way that it deals with mistakes. It's actually reasonably consistent and makes sense, but in order to attain that consistency the law has to be somewhat complex.
Basically, there are two kinds of mistake: Mistakes about the law and mistakes about the facts. And for our purposes, there are two major crime categories to examine with respect to mistakes: Actual crimes and attempted crimes. Whether mistake will be an excuse depends on whether it was a mistake of facts or a mistake of law, and whether the crime succeeded or was merely attempted. Broadly speaking, if a crime actually occured a mistake of facts can get you off; a mistake of law won't. On the other hand, if a crime was merely attempted, a mistake of facts won't save you, but a mistake of law will. This sounds confusing and contradictory, but it makes sense
To start, understand that a crime generally requires two elements: an actus reus and a mens rea. An actus reus is a criminal act, the thing in itself, the real-world action that we have labelled criminal. Mens rea is a guilty mental state. You have to have been thinking culpable thoughts as you commited the actus reus in order to be guilty of a crime. A simple example: I am standing at the airport, and have set my bag down. I'm looking around for my gate, slightly lost. You walk by, casually pick up my bag, and walk off. I turn around and notice it's gone. Taking my bag was an actus reus; it's my property, and you took it without my permission. This is all very objective and easy. You've committed all the action you need to be guilty of a crime. But! The crime of theft isn't simply taking another's property, it's knowingly taking another's property. Suppose that you have a bag just like mine. You set it down somewhere else to go ask a directions to your gate. Thinking my bag was yours, you picked it up and walked off. Even though you committed the act of taking someone else's property, you didn't know you were taking someone else's property. You lacked a culpable mens rea, so you're not guilty of a crime.
And that's a classic example of a mistake of fact. The rule is that you're not guilty if you wouldn't have been guilty if the circumstances had been as you believed them to be at the time. At the time, you believed the bag was yours. If the bag had been yours, you wouldn't have been guilty. Therefore, your mistake of facts exonerates you. To complicate things, suppose you made a mistake of fact, but still had a guilty mens rea. Suppose you know a guy flying out of town named Jim who has a bag just like mine. You came to the airport to steal Jim's bag. I look like Jim from behind, and you thought it was Jim's bag. You stole it, only to discover you got the wrong bag. In this case, your mistake of facts doesn't exonerate you. Again, the court asks "if the world were as the defendant believed it to be, would she have been culpable?" You believed the bag was Jim's. If it had been Jim's and you had taken it, would you have been guilty of a crime? Yes. So a mistake of fact can generally get you off, but not always.
But mistake of law will almost never get you off. Suppose you've just moved to a state where it's illegal to buy alcohol on Sundays. You don't know about this law and somehow manage to buy a six-pack of beer without anyone telling you about the law (leave aside the improbability of finding a place that will sell you alcohol on Sunday without your realizing that it's some sort of black market transaction). You're arrested and plead that you made a mistake. Here, you made a mistake of law, not of facts; you didn't know what you were doing was illegal, but you knew that you were buying beer and that it was Sunday. The courts don't care. Ignorance of the law, as they say, is no excuse. The reasoning behind this rule is that if you could get off by not knowing what the law is, everyone would have an incentive to keep themselves willfully blind to the law. That's no way to run a well-ordered society. Moreover, citizens, the courts have decided, have an affirmative duty to apprise themselves of the law and conduct themselves accordingly.
Nonetheless, it is possible, though unlikely, to get off by arguing mistake of law. If a statute specifically requires a knowing violation of the law, failing to know the law prevents you from having the necessary mens rea. This presents a really tricky question with respect to modern regulatory statutes. A lot of times Congress will punt to administrative agencies to make their rules for them, and they'll pass laws saying, "We give authority to the Department of Such-and-Such to make all rules regulating gobbledygook. Anyone who knowingly violates the Department of Such-and-Such's rules on gobbledygook is guilty of a Class Q Misdemeanor." The crime is "knowingly violating the rule." So if you take an action that violates the rule, and know that you're taking that action, but don't know what the rule is, did you knowingly violate the rule? The Supreme Court's answer is: Maybe. About half the time they've said "Yes, that is a violation," and the other half they've said "No, it isn't." It seems to depend a lot on how obscure the regulation is and how reasonable it is for this particular defendant to know what the law is. Complex regulations of the importation of various metals: the companies are liable whether they know the regulations or not, because they're businesses in that field and can be expected to familiarize themselves with the law. Complex regulations of the use of food stamps: families aren't liable if they don't know the rules, because it's unrealistic to expect an impoverished family to understand a complex set of regulations like that.
You can also generally argue Mistake of Law if it's a very new law that hasn't been promulgated publically, but that's pretty hard to do these days when government printers can publish laws as soon as they're passed. Finally, you may get a mistake of law defense if you've personally inquired of the highest interpreter of a given law, rule, or regulation and they give you a bogus interpretation. So if you write the state Attorney General and ask whether it's alright to buy beer on Sundays, and he writes back and offers the official interpretation, "Sure, why not?" you can use that to argue that your mistake as to the law was fully justified, since you relied upon the official interpretation of the highest enforcer of the state's laws. Note that this has to be the highest interpreter (your local cop's opinion doesn't matter), it has to be an official opinion (something you overheard at a party won't work), and the law can't have changed materially since you got the interpretation (if you got the Attorney General's opinion last week, and the legislature passed the No Alcohol on Sunday Law this week, the Attorney General's opinion is no longer operative).
That covers mistake with respect to actual crimes. What about mistake with respect to attempted crimes? Here the presumptions are reversed: mistakes of fact generally won't save you, but mistakes of law will. Again, buying alcohol on Sunday. You know it's a crime, but you decide to go ahead and do it anyway. You march into a liquor store, grab a six pack, and openly and nefariously present it to the cashier. He makes the sale and you walk out, only to be arrested. The cops take you to the interrogation room, you waive your 5th Amendment rights, and you brag about buying the beer. "Sure, I wanted to buy beer. It's a stupid law and I'm going to violate it every chance I get until they change it!" But it turns out that you screwed up. In the midst of your heroic violation of an unjust law, you accidentally picked up a six-pack of non-alcoholic beer, which is completely legal. So you knew what the law was and you consciously chose to violate it, but you failed because of a mistake of fact. Guilty?
According to the Common Law and the Model Penal Code, yes. Again, we examining the world as you believed it to be. You believed the beer was alcoholic, and that it was Sunday. You bought it anyway. If that had been how things were, you'd have been guilty. Therefore, you're still guilty even though things weren't as you thought.
So what about the requirement of an actus reus? You've got a guilty mental state, but not a criminal act attached to it. That's where the law of attempts comes in. You're not just guilty if you commit a crime, you're also guilty if you attempt to commit a crime (though you'll generally be punished at a lower level). Attempts, though, also require an actus reus; it's not enough to just think about committing a crime, you have to do something about it. What acts are necessary to constitute an attempt is a matter of considerable debate, and different states have different standards, but generally you need to have taken a substantial step towards committing the crime. In this case, you've done everything you need to commit the crime, if the world had been as you believed it to be, and therefore you've committed all the actus reus you need to in order to be guilty of attempted purchasing alcohol on a Sunday.
Finally, mistake of law for attempted crimes. Now suppose that you once lived in the state with the alcohol law, but just moved to another state that is less puritanical. Your new state has no problem with you buying alcohol on Sunday, but you mistakenly believe that the alcohol ban is enforced everywhere. But one Sunday you can't take being without alcohol any longer and decide to go buy some beer. You're surprised at how easy it is, but your guilty conscience gnaws away at you. After drinking the full six-pack, you decide, in a melancholy and inebriated state, to clear your conscience by turning yourself in. You write out a confession and present yourself to the local police precinct. In this case, you've made a mistake of law. You set out to commit what you believe is a crime, you committed what you believe is a crime, but it turns out that it isn't a crime. Guilty?
In most states, no. I mean, it presents the interesting question of how, exactly, they would punish you. What would a judge do with you? How would he derive an appropriate sentence for violation of a law that isn't on the books, has never been on the books, and really exists only in your head? Moreover, there's a strong principle in American jurisprudence that you can only be punished for violating crimes that have been promulgated as statutes. You can't be guilty of a common law crime, one created by judicial decisions alone. Similarly, it seems vaguely wrong to punish someone for an unpromulgated law that exists only in their own mind.
Note that this standard doesn't meet the "if the world were as you believed it to be" test, since the opposite standard would fail to meet the pre-requisite "does this standard make any goddamn sense at all?" test. Nonetheless, in the interest of consistency the Model Penal Code, which hasn't been fully adapted by any state, and to my knowledge has not been adopted in this provision by any state, has denied that Mistake of Law should excuse an attempt. So the MPC would impose a punishment for buying the beer if you thought it was a crime. The MPC has a decent theoretical justification. You have the mens rea, you're culpable, and you carried out your plan to violate the law. You're therefore the sort of person who wants to violate the law and acts on those desires, so you should be punished whether it's actually a law or not. Nonetheless, as discussed, such punishment would be hard to work out in practice. Plus, it's pretty unlikely that surreptitious breaches of non-existent laws will be caught, so it doesn't come up all that often.