In logic, and especially in its applications to mathematics and philosophy, a counterexample is an exception to a proposed general rule or law. For example, consider the proposition "all students are lazy". Because this statement makes the claim that a certain property (laziness) holds for all students, even a single example of a diligent student will prove it false. Thus, any hard-working student is a counterexample to "all students are lazy". More precisely, a counterexample is a specific instance of the falsity of a universal quantification (a "for all" statement).
In mathematics, this term is (by a slight abuse) also sometimes used for examples illustrating the necessity of the full hypothesis of a theorem, by considering a case where a part of the hypothesis is not verified, and where one can show that the conclusion does not hold.
In mathematics, counterexamples are often used to prove the boundaries of possible theorems. By using counterexamples to show that certain conjectures are false, mathematical researchers avoid going down blind alleys and learn how to modify conjectures to produce provable theorems.
Suppose that a mathematician is studying geometry and shapes, and she wishes to prove certain theorems about them. She conjectures that "All rectangles are squares". She can either attempt to prove the truth of this statement using deductive reasoning, or if she suspects that her conjecture is false, she might attempt to find a counterexample. In this case, a counterexample would be a rectangle that is not a square, like a rectangle with two sides of length 5 and two sides of length 7. However, despite having found rectangles that were not squares, all the rectangles she did find had four sides. She then makes the new conjecture "All rectangles have four sides". This is weaker than her original conjecture, since every square has four sides, even though not every four-sided shape is a square.
The previous paragraph explained how a mathematician might weaken her conjecture in the face of counterexamples, but counterexamples can also be used to show that the assumptions and hypothesis are needed. Suppose that after a while the mathematician in question settled on the new conjecture "All shapes that are rectangles and have four sides of equal length are squares". This conjecture has two parts to the hypothesis: the shape must be 'a rectangle' and 'have four sides of equal length' and the mathematician would like to know if she can remove either assumption and still maintain the truth of her conjecture. So she needs to check the truth of the statements: (1) "All shapes that are rectangles are squares" and (2) "All shapes that have four sides of equal length are squares". A counterexample to (1) was already given, and a counterexample to (2) is a non-square rhombus. Thus the mathematician sees that both assumptions were necessary.