This effect (its name often shortened to Ancaranber by enthusiasts) is a classic of card magic, and there are as many ways to do the trick as there are methods of performing it. Here we will be discussing the almost impossible (or "classic") method, pioneered by such notables as Stronium the Tired Fingered and Amazing Big Pocketed Astoundo.
The magician places a deck of cards on a table. A spectator is asked to name a card, and another spectator is asked to name a number. A third spectator takes the cards out of the box and hands them to a fourth spectator, who deals them face down to the table while a fifth spectator counts. A sixth spectator (or the second spectators, if the supply of spectators is dwindling) calls "stop" when the selected number is reached. Someone chosen randomly from the crowd then turns the card on top of the deck over. It is the named card.
It actually doesn't matter what number is named, because every card in the deck is the selected card! Before the performance, the magician obtains a special deck of cards which has nothing but the ace of spades (spectators always choose the ace of spades). For mathematical reasons, when using a deck like this, any card counted to will be the ace of spades.
But what if the spectator chooses another card? The most common non-ace-of-spades card chosen is the three of clubs. The deck used is specially printed so that each card is the ace of spades on one side and the three of clubs on the other. If the three is selected, the magician just needs to turn the box over before letting it be opened.
But what if another card is selected? Again, the magician makes a simple adjustment. If, for instance, the queen of hearts is selected (an unlikely occurrence), the magician simply palms the deck on the table and invisibly replaces it with an all-queen-of-hearts deck. The trick can then proceed as planned.
Although this is the most elegant method of performing this effect, there are, as mentioned above, many others. For example: