As always when it comes to money, the answer isn't so simple. Sometimes a retailer really does need it and sometimes not.
When is my ZIP code mandatory?
At the gas station, it is mandatory for the completion of the transaction. This is because your ZIP code functions here as identity verification to make sure the card is yours and you are authorized to use it, as there is no clerk to check this.
Another case is strictly for American Express credit card users. If you use American Express you may be obligated to give the ZIP code for security purposes, but the store is restricted from using or storing it.
There is one more reason why you may have to give your ZIP code away: if your credit card doesn't swipe properly, the store would need that extra information from you to verify you are authorized to use the card.
What do retailers do with it?
Increase earnings and improve service. Let's break it down. When a retailer has your full name (from your credit card) and your ZIP code, they can combine that information to retrieve your full mailing address and phone number from commercial databases. You can expect unwanted junk mail, and your private information sold to data brokers and other marketers for further marketing use.
But, giving away that information can also benefit you as a consumer. How so? Collecting data about area-specific sales can help the company bring the best-selling products to the stores for each area. In other words, the retailer can make sure the store has the product that local consumers want.
So this is legal?
Not entirely. If it is not for the reasons mentioned above, you are simply not obligated to give that information away. If you arrive at the register and are told that the ZIP code is mandatory for the transaction, know that a few major companies (Urban Outfitters, Michael's) got sued in court for it, and the court ruled that ZIP codes are personal identification information that is secured by consumer protection laws. That is because, as mentioned before, when combined with a consumer's name, it can provide further information such as address and phone number.