They often earn a couple percentage points per transaction so the hope is that you use the card for more than the 5% categories, which is a loss leader for them (this ignores the finance charges, fees, and other charges they impose for a variety of reasons).