Well it isn’t free even if you don’t carry a balance - every store that accepts the card pays a fee for each and every transaction the card is used for. That’s why they entice you to use the card even more with cash back bonuses even if you never carry a balance. Those that do that not only don’t pay anything for the card, but get paid for using it.
You are correct, in that the stores pay a fee to the card companies. I think it’s around 3%, which is why the card companies can compete with each other with cash back offers.
Usually, that processing fee is invisible. But, sometimes the cash and credit prices are different, especially in low margin businesses like gas stations.
I once negotiated a good cash price on a car, and pulled out my credit card. No go... They wanted a check that would clear before pickup to avoid the Visa or MC processing fee. I couldn’t blame them, but thought it would be fun to see what they’d do when I offered to pay that way.