According to Phrase Finder, this is one of many derogatory phrases that became popular in the 1980s to indicate someone “having a bit missing,” or “not all there,” as would be a person playing cards without the full deck.
I’ve read a theory that the expression came from a time when a tax charged was charged on the ace of spades and that people chose to play with 51 cards with that card missing to avoid the tax. Wikipedia has a slightly different story. Charles I of England extended stamp duty to playing cards in 1711. One of the cards in the pack, and usually the ace of spades, was marked with a hand stamp. Another story was that some decks of cards were made without aces because the members of the royal court didn’t want a card that outranked the king and queen.