Counterfactual reasoning is essential to mathematics. A mathematician might assume some dubious hypothesis, derive an absurd result, and conclude that the hypothesis must be wrong. The method is called reductio ad absurdum. For example, the Greeks proved that there are infinitely many prime numbers as follows. If there were some largest prime N, then all larger numbers could be divisible by primes equal to N or smaller. But you could multiply all those primes together and add one, and get a contradiction. Therefore there must be prime numbers larger than N.
A more trivial example of this reasoning is to prove that there is no largest integer. For any integer N, the integer N+1 is larger. So assuming that there is a largest integer quickly leads to a contradiction. They also used an argument by contradiction to show that the square root of 2 is irrational -- that it cannot be written as a fraction a/b. If it could be, then a2 = 2b2. But 2 is a factor an even number of times on the left, and an odd number of times on the right, a contradiction. That is why the decimal expansion of the square root of 2 is an endless and non-repeating sequence of digits.
Counterfactual reasoning is also used to create new numbers. Even tho there is no rational square root of 2, we can just hypothesize one anyway. We have to expand our notion of number, and no longer require that a number is a ratio of integers. We still have many other properties, such as every number being less than, equal, or greater than other numbers. We can also lose that property by allowing a counterfactual square root to -1. Other counterfactuals give us infinitesimal numbers and non-Euclidean geometry.
Mathematical logic carefully distinguishes the counterfactual from the false conditional. A statement of the form "if A then B" is always true if A is a false conditional. That is, a false hypothesis allows you to prove anything. For example, if 0 = 1, then I am the Pope. There is no use to a truly false conditional. The value is in a hypothesis that might be true on some alternative universe of mathematical facts.
The statement about the Pope may be proved as follows. If 0 = 1, then add 1 to both sides, getting 1 = 2. The Pope and I are 2 different people. But 2 = 1, so we are 1 person, and I am the Pope. Or you can prove it without this sort of cleverness by just appealing to the meaning of a conditional.
A mathematician might also say, "if pigs have wings, then I am the Pope." That is because pigs do not have wings. If you want to posit some counterfactual world in which pigs have wings, then you need a grammatical modifier or mood to show that.
Sometimes even mathematicians are unhappy with counterfactual reasoning. A century ago L. E. J. Brouwer proved that any continuous function from the disk to itself must have a fixed point, but his proof relied on counterfactual reasoning and gave no way to find such a point. He adopted the view that such a theorem is dubious unless it gives a way to construct the solution.
Mathematician Terry Tao explains his version of counterfactual reasoning in three posts on The “no self-defeating object” argument. As he explains, many elementary theorems are very confusing to students because they are based on constructing some impossible object. There can be no impossible object, so the proof seems like a paradox. The contradiction is avoided by precise mathematical definitions and analysis.
As a simple example, Tao proves that there is no largest integer. The proof is trivial, as any largest integer N would be less than N+1. But the counterfactual subtlety of this argument is sufficient that a child is more likely to learn it from another child on the playground, than from a teacher or textbook.
A very widely promoted AT&T TV commercial focuses on this proof as a way of arguing that bigger is better in phone networks. Sure enough, the kids grasp the concept as the teacher has a difficult time explaining it.
Thus the counterfactual is one of the core concepts in mathematics. You will never learn proofs without it.