Origin of "spill the beans"
Random House: http://www.randomhouse.com/wotd/index.pperl?date=20010223
The OED gives a 1574 quote for spill it meaning 'to divulge, let out': "Although it be a shame to spill it, I will not leaue ['omit'] to say that which..his friends haue said vnto me."
The spilling of beans endures, within and outside the U.S. (...) Over the years, there have been countless variations of form and meaning, all with spill. (...) And although we have tracked spill, we still don't know beans.
A popular folk etymology for spill the beans claims that in ancient Greece, applicants for membership in secret societies were voted upon by having the existing members drop beans into an opaque pottery jar.
Etymonline says that this is a rather recent phrase, first recorded in 1919. The Phrase Finder supplies two slightly older quotes.
The earliest uses of 'spill the beans' come from the USA. The meaning of the phrase was then something like 'spoil the beans' or 'upset the applecart'[.] The first example I can find is from The Stevens Point Journal, June 1908 [.]
Soon after that the phrase was used with the meaning of 'upset a previously stable situation by talking out of turn', which is close to how we use it today. That is cited in The Van Wert Daily Bulletin, October 1911:
Finally Secretary Fisher, of the President's cabinet, who had just returned from a trip to Alaska, was called by Governor Stubbs to the front, and proceeded, as one writer says, to 'spill the beans'.
Note the quotation marks, which suggest that this wasn't an established phrase back then.
Summary
Spill the beans was used in horse-racing in St. Louis, MO, as early as 1902 and meant "to cause an upset". By 1907 it was being used with a similar meaning in baseball in other states.
1902 horse-racing
The OED has spill the beans meaning "to reveal a secret" from 1919. The Phrase Finder has 1908 for the meaning of "upset the applecart" and 1911 for "upset a previously stable situation by talking out of turn".
The earliest I found is a 1902 beans were spilled in horse-racing, meaning "caused an upset", quoting owner E. J. Arnold.
The St. Louis Republic (St. Louis, MO), November 25, 1902:
"Ethylene was 15 to 1 one day and would have won sure had Battiste been up. He was set down by the starter in St. Louis and I had to ride a bad boy. Then we put Battiste up later and got down. Kiley told him to take her back a bit. He did, and in some manner the field ran around and over her so that she was shut in, cut off and lost. So the beans were spilled.
1903 horse-racing
I found some 1903 examples, all from horse-racing meaning to cause an upset, and all in the same The St. Louis Republic.
The St. Louis Republic (St. Louis, MO), May 06, 1903:
KINGSTELLE SPILLED THE BEANS.
Every one fancied that the fifth race was a two-horse one between Nearest and Audiphone, who were held at 4 to 5 and 8 to 5 respectively. Kingstelle, a 10-to-l shot, broke it up. She laid away from the pace and came along in the stretch, and won, handily, a real nice race. Nearest and Audiphone ran to a head finish. Nearest getting the best of it. It was the third time that these two horses met on equal terms. Each time Nearest has beaten Audiphone by a head. This is form of a miraculous nature. The secret of it is that both have been trying hard. Neither Mr. Flippen, who trains Audiphone, or Mr. Hughes, who owns Nearest, is around stalling with the old complaint: "Now, you know you feel good one day and bad the next. That is just like a horse."
The St. Louis Republic (St. Louis, MO), June 13, 1903:
St. Agnes II has no business in here. Out with Bel Coeur, a full sister to the French girl. Her Folieship has no business in either, as she is liable to run out and spill the beans.
The St. Louis Republic (St. Louis, MO), September 07, 1903:
ATTRACTIVE CARD AT DELMAR RACES.
Each Race Presents a Horse That Seems to Hold His Field Safe.
STAKE RACE IS THE HARDEST.
Jordan Should Win on Form, but Helen Print and W. B. Gates Likely to Improve and Spill the Beans.
The St. Louis Republic (St. Louis, MO), September 09, 1903:
He is not a consistent horse, judging from his races. Neither did he get much of a ride last time, when it seemed that Inglethrift was the "right" one. Lord Hermence came on and spilled the beans.
1907 baseball
By 1907 the phrase had spread beyond Missouri and beyond horse-racing.
Here's baseball in The Democrat-Sentinel (Logan, Ohio), 29 Aug. 1907:
At this point the game began to get interesting, as Logan was just two scores behind, and were beginning to find Farrow's delivery with ease, but the beans were spilled in the eighth when Jones, who played sensational ball all during the game, erred on Murphy and Kelley, first two up, this followed by stolen base and an error by Johnson at the plate and Farrow's single were good for thee more runs, bringing their total to nine, just three more than Logan made during the entire game.
Here's a pun in The Rice belt journal (Welsh, Calcasieu Parish, LA), 23 Aug. 1907 (also in VA):
QUERULOUS QUICK-LUNCHER
Eat in haste and repent in pepsin.
Do not eat with your knife; it spills the beans.
A penny saved is a penny earned by the doctor.
It was still used in horse-racing in 1907 (L.A. CA).
And beyond
As The Phrase Finder shows by 1908 it was used outside sport meaning "to cause an upset". And by 1911 it was used in politics meaning "upset a previously stable situation by talking out of turn". World Wide Words has a 1910 "extension of the sports sense into upsetting a situation by speaking out". From there, the meaning evolved to the modern one, to reveal a secret which may cause an upset.