It's a fallacy because at no point is it shown that A is the only possible cause of B; therefore, even if B is true, A can still be false. For example
If my car was Ferrari, it would be able to travel at over a hundred miles per hour. I clocked my car at 101 miles per hour. Therefore, my car is a Ferrari.
This is popular in conspiracy theories. Here the fallacy is fairly obvious; given the evidence, the car might be a Ferrari, but it might also be a Bugatti, Lamborghini, or any other model of performance car, since the ability to travel that fast is not unique to Ferraris. Hell, it might even be a Subaru Outback. Note that while this may appear to call all hypothesis / evidence experiments fallacious, they are based on additional evaluations of the likelihood of other theories, thus establishing that A is a likely cause of B.
The flip side of the above, where you say that because the initial conditions did not happen, the result is impossible.
If a person is wearing a hat, they have a head. I am not wearing a hat. Therefore I do not have a head.
Note that, by the contrapositive rule, these two fallacies are equivalent. For example, you could replace "If a person is wearing a hat, they have a head" by the logically identical statement "If a person has no head, they aren't wearing a hat" to turn the first example of denying the antecedent into an example of affirming the consequent.
In American Dad!, Stan sinks his entire savings to build a rocket for Steve to win a contest.
Stan: You gotta spend money to make money. Francine: But you didn't make any money! Stan: So logically, I didn't spend any money! *waves at the camera* Goodnight everybody!
In Yes, Prime Minister, Sir Humphrey Appleby explains the fallacy and how foolishly people can fall for it. He demonstrates the illogic of the fallacy by saying:
All cats have four legs. My dog has four legs. Therefore my dog is a cat!
Inference to the best explanation. The usual form of scientific reasoning, as well as a lot of Sherlock Holmes' "deductions" (though he's wrong to call them that, since this is a form of inductive reasoning).
B. The best explanation for B would be A. Therefore, A (probably).
This differs from the Ferrari example above in that it posits a stronger connection between A and B than just A's truth entailing B's; B is actually giving some positive reason to prefer A over the other possibilities. Also, this form of argument isn't claiming deductive certainty, so the bar is a little lower for it being acceptable.
Scientific reasoning is frequently attacked by those who understand this fallacy, but not the scientific method, which has the following form:
B. A is the best explanation for B, so I will claim "A is the most likely explanation." If A, then C. Therefore, if not C, not A (valid contrapositive). Is C true? Yes? I will increase my confidence that A is the correct explanation. If A, then D. Not D! I must provisionally reject A or modify it to account for D, then continue to seek new information and propose new possible explanations.