How does magnets in credit cards work?

There are no magnets in credit cards. If you're referring to the 'magnetic strip' on the back of the card - it's a secondary system of identifying the card. When you insert your card into an ATM - the machine reads the magnetic strip which connects it to the account-holder's bank. Contrary to popular belief - it does not contain the PIN associated with the card.