A credit or debit card will fail to swipe correctly for one of three reasons.
First, something has permanently interfered with the magnetic strip on the card, so that the computer cannot read it. The cashier will have to type in the number manually, and a new card will probably need to be issued.
Secondly, the machine is faulty and is unable to read the card.
However, the third reason the card cannot be read is the most common cause. Dust or dirt of some sort has collected on the magnetic strip. This obscures the information from the electronic reader.
A quick wipe on your sleeve is all that is required to resolve this and, in the vast majority of cases, the card will swipe successfully at the second attempt.
There is no great mystery and no big science behind the practice of rubbing the magnetic strip of a credit card.
If you keep your cards in the card compartment of a purse or wallet, they should remain reasonably clean, and swipe easily on the first attempt. This should also eliminate the first problem, because they will be protected from anything that is likely to irreversibly damage the strip.
There is one drawback to rubbing the magnetic strip on a debit or credit card.
Rubbing the card can sometimes make it more difficult to read because it becomes charged with static electricity that can interfere with the electronic reader.
The instinct to rub the card in an attempt to remove any dust that may have stuck to it may work in the short term but the extra static charge the rubbing has generated will ensure that even more dust will cling to the card later on.