A necessary caution first
This article is a general explanation, not legal advice. Laws about gambling differ between countries, regions and sometimes individual venues, and they change over time. Anyone who needs a firm answer for a particular place should check the local regulations or consult a qualified professional. What follows describes common patterns in broad terms.
Counting in your head
Card counting, in the sense of mentally tracking which cards have been dealt, uses only information that is openly visible to every player. Nothing is hidden, marked or altered. In many places, this is not treated as a crime in itself. The usual reasoning is that observing the game and thinking about it is not cheating, because the player is applying judgement to public information and does not change the cards or the rules.
That said, being not a crime does not mean being welcome. Gambling venues are generally private businesses, and the law in many places allows them to decide who may play at their tables.
Why casinos can still say no
A casino typically has the right to refuse service within the limits of local law, much as other private businesses can. In practice, this tends to mean that a venue may ask a player to stop playing blackjack, to play only with flat bets, or to leave. That is a business decision about the game, not a finding of wrongdoing. The casino countermeasures in use range from reshuffling early to asking someone to leave.
Rules and attitudes vary between venues and over time, and some places may be bound by regulations that limit what a venue can do to a customer. This is one reason a general article cannot say what will happen at any given table.
Where devices change the picture
Using a device or outside aid to help with counting is treated very differently in many jurisdictions. Some places specifically prohibit electronic or mechanical aids at gaming tables, and breaking those rules can be a criminal matter rather than a simple refusal of service. The same goes for any attempt to work with an employee or to alter the cards. Those activities are cheating, not counting.
If you are curious about the difference, a simple rule of thumb is that using only your own observation and memory is a different category from using equipment, but the exact line depends on local law, so it should be checked rather than assumed.
Online and live dealer games
The legal position can look different away from a physical table. In online blackjack with a random number generator, the cards are effectively drawn from a fresh set for each round, so counting has no meaning. Live dealer games may deal from a shoe, but the operator’s terms of service also apply and can restrict certain behaviour. In every case, the terms of the site matter alongside local law.
Why legality is only part of the question
Even where nothing is illegal, counting runs into practical issues. Venues can end play. The theoretical edge is small, swings are large, and mistakes can erase the advantage. Many tables are structured so that opportunities are limited. Whether something is permitted and whether it is sensible are separate questions, and the second one deserves more weight than most stories give it.
It is also worth remembering that blackjack has a house edge in ordinary play. Anyone thinking of counting as a way to make money should understand that outcomes remain uncertain, that past hands do not change future odds in a way that guarantees a win, and that gambling can be harmful.
A sensible approach
If you are curious about counting as mathematics, learning how it works carries no legal risk by itself. If you intend to play, read the house rules and the local regulations, play only with money you can afford to lose, and be prepared for a venue to decline your play. If gambling is causing stress or difficulty, a local support service can offer confidential help.