The Wikipedia entry for Foobar covers this pretty thoroughly (emphasis mine):
The terms foobar, foo, bar, baz and qux are sometimes used as placeholder names (also referred to as metasyntactic variables) in computer programming or computer-related documentation. They have been used to name entities such as variables, functions, and commands whose purpose is unimportant and serve only to demonstrate a concept. The words themselves have no meaning in this usage. Foobar is sometimes used alone; foo, bar, and baz are sometimes used in that order, when multiple entities are needed.
The origins of the terms are not known with certainty, and several anecdotal theories have been advanced to identify them. Foobar may have derived from the military acronym FUBAR and gained popularity because it is pronounced the same. In this meaning it also can derive from the German word furchtbar, which means awful and terrible and described the circumstances of the Second World War.
FOO is an abbreviation of Forward Observation Officer, a British Army term in use as early as the First World War. The etymology of foo is explored in the Internet Engineering Task Force (IETF) Request for Comments 3092, which notes usage of foo in 1930s cartoons including The Daffy Doc (with Daffy Duck) and comic strips, especially Smokey Stover and Pogo. From there the term migrated into military slang, where it merged with FUBAR. "Bar" as the second term in the series may have developed in electronics, where a digital signal which is considered "on" with a negative or zero-voltage condition is identified with a horizontal bar over the signal label; the notation for an inverted signal foo would then be pronounced "foo bar". Bar may also be read as beyond all repair, which is how it is used in the acronym FUBAR.
The use of foo in hacker and eventually in programming context may have begun in MIT's Tech Model Railroad Club (TMRC). In the complex model system there were scram switches located at numerous places around the room that could be thrown if something undesirable was about to occur, such as a train going full-bore at an obstruction. Another feature of the system was a digital clock on the dispatch board. When someone hit a scram switch the clock stopped and the display was replaced with the word "FOO"; at TMRC the scram switches are therefore called "Foo switches". Because of this an entry in the 1959 Dictionary of the TMRC Language went something like this: "FOO: The first syllable of the misquoted sacred chant phrase 'foo mane padme hum.' Our first obligation is to keep the foo counters turning."
One book describing the MIT train room describes two buttons by the door: labelled foo and bar. These were general purpose buttons and were often re-purposed for whatever fun idea the MIT hackers had at the time. Hence the adoption of foo and bar as general purpose variable names.