Excerpted from the Jargon File:
When ‘foo’ is used in connection with ‘bar’ it has generally traced to
the WWII-era Army slang acronym FUBAR (‘Fucked Up Beyond All Repair’
or ‘Fucked Up Beyond All Recognition’), later modified to foobar.
Early versions of the Jargon File interpreted this change as a
post-war bowdlerization, but it it now seems more likely that FUBAR
was itself a derivative of ‘foo’ perhaps influenced by German
furchtbar (terrible) — ‘foobar’ may actually have been the original
form.
For, it seems, the word ‘foo’ itself had an immediate prewar history
in comic strips and cartoons. The earliest documented uses were in the
Smokey Stover comic strip published from about 1930 to about 1952.
Bill Holman, the author of the strip, filled it with odd jokes and
personal contrivances, including other nonsense phrases such as
“Notary Sojac” and “1506 nix nix”. The word “foo” frequently appeared
on license plates of cars, in nonsense sayings in the background of
some frames (such as “He who foos last foos best” or “Many smoke but
foo men chew”), and Holman had Smokey say “Where there's foo, there's
fire”.
If this sort of etymological study is of interest to you, there's a vast wellspring more of it at the link.
But what's known for certain is that the modern usage of foo (and bar, and much other modern computer nonsense jargon) is first documented as appearing at MIT in the late 1950s. Which bits of jargon the MIT engineers/students picked up from elsewhere (say, from a popular comic strip from six or seven years earlier), and which they invented themselves is much less well-documented. Hints about a history pre-dating the MIT Tech Model Railroad Club are intriguing, but are also mostly supposition and anecdotes.