"Mistake of fact" generally refers to a mistaken understanding by someone as to the facts of a situation—the mistake results in the person committing an illegal act. Mistake of fact is a defense to a crime where the mistaken belief, if it were true, would negate a mental state that's an element of the crime.
Will any mistake do? No. In most situations, the mistake must be reasonable. But even an unreasonable mistake of fact can at least at times provide a defense if it negates a required specific intent.
Example: Kaitlin takes her Labrador, Buster, to the dog park every day so he can play off leash with the other dogs. On Friday, Kaitlin lost sight of Buster for a few minutes. When she relocated him, she put him on the leash and walked him home. At home, Kaitlin took a close look at Buster and noticed he had a different collar with the name "Spot" on it. Kaitlin had mistaken Spot for Buster because they are both Labradors and look just alike. Kaitlin isn't guilty of theft because, when she took Spot home, she made a reasonable mistake of fact—that Spot was her dog.
Example: On Saturday, Kaitlin again takes Buster to the dog park. Once again, she loses track of him for a few minutes. This time, when Kaitlin goes to look for Buster, she is more focused on texting with her friend Nancy than on finding Buster. Kaitlin puts a leash on a dog she believes to be Buster, without looking down at the dog, and walks home. When she arrives, she looks down at the dog for the first time, and is shocked to see it's a Chihuahua. Although Kaitlin's mistake was clearly unreasonable—a Chihuahua looks nothing like a Labrador—it may still provide her with a valid defense. That's because the mistake negates the specific intent required for theft. Theft typically requires that the defendant specifically intend to permanently deprive the owner of property. (Many crimes don't fall into the specific intent category—check out General Intent Crimes vs. Specific Intent Crimes.)
Typically, mistake of fact is a regular defense, rather than an affirmative defense. In other words, where relevant, the prosecution must prove beyond a reasonable doubt that the defendant acted with criminal intent rather than through reasonable mistake. The defense can argue that the defendant acted because of a reasonable mistake, leaving the prosecution to establish otherwise.
Although mistake of fact is typically a legitimate defense, it's unlikely to aid in defending against a strict liability crime. Courts have also generally rejected mistake of fact where the mistake was unreasonable but attributable to the person's mental illness. (The law on issues like insanity and "diminished capacity" is complex and varies—for more information, see Defenses to Criminal Charges.)
The difference between mistake of fact and mistake of law is often subtle, and courts vary in how they handle such mistakes. If you have been arrested or charged, you should talk to a qualified attorney in your area to find out how the law applies to the facts of your case.