It originated from a trick where a pig was sold in a bag, but a cat was secretly substituted. If the buyer opened the bag before paying, the cat would escape, revealing the trick.
So, if someone "lets the cat out of the bag," they are giving away information that was meant to be kept secret.