In the US, and likely most countries, card counting is not illegal. However, casinos can refuse to do business with anybody they want, so they can ban you for it. The exception is Atlantic City where you can't be banned for card counting, but if you are suspected of it, they will use other tactics to negate any advantage you may have (e.g. shuffling after every hand)