The term originates in the Age of Sail and wooden men-of-war, apparently first used in Victor Hugo’s novel Ninety Three in 1874. When a storm began, all cannon had to be securely fastened and lashed in place. A gun that broke free of its lashings would roll uncontrollably around the deck with the motion of the ship, causing havoc. A loose cannon, weighing thousands of pounds, would crush anything and anyone in its path, and possibly even break a hole in the hull, thus endangering the seaworthiness of the whole ship. If a loose cannon fired in the wrong direction, it could severely damage the ship or kill crew members.
The first figurative use appears to be in The Galveston Daily News in December 1889 where it was used to describe the potential (but unmaterialized) power of the local African-American voting block.
In modern usage, it is used almost exclusively to refer to individuals with largely uncontrollable and thus potentially destructive behavior, but it can also be proudly self-descriptive. It is also a fictional superhero entitled Loose Cannon (comics).