Three forms of the 'moon' and 'shot' compound appear in OED Online. One of those, "moon-shot adj. illuminated by moonlight, esp. fitfully" is neither pertinent to the question nor in the same lineage, although apparently older than the other two, and so I ignore it.
Another, 'moonshot' the closed compound, has acquired the figurative sense you mention in your question: "[an attempt] to achieve a lofty if not impossible goal". The term is defined literally as
orig. U.S.
The launching of a spacecraft to the moon; (also) the spacecraft itself.
["moonshot, n.1". OED Online. September 2016. Oxford University Press. http://www.oed.com/view/Entry/254405?rskey=K81dkE&result=2&isAdvanced=false (accessed October 11, 2016).]
The earliest attestation shown is from 1949:
Rotarian Apr. 54/2 The real result of all the work which would have to go into the moon shot..would be the knowledge of how to build and operate rockets of such size.
(op. cit.)
OED Online also provides a definition and attestation for the open compound, 'moon shot':
Baseball.
A type of hit characterized by its great height; esp. one which sends the ball over the left-field fence, scoring a home run.
["moon shot, n.2". OED Online. September 2016. Oxford University Press. http://www.oed.com/view/Entry/254171?rskey=1GV25g&result=3&isAdvanced=false (accessed October 11, 2016).]
The compound with reference to baseball is attested from 1969 in Sports Illustrated. Note that OED attestations are not intended to rule out earlier origins; instead they provide early clear textual evidence of the term used with the specified sense. The etymology of the open compound is given as from
the name of Wallace (Wally) Moon ... renowned for his technique of hitting the ball over the left-field boundary fence + shot n.1. Perhaps influenced by moonshot n.1
(op. cit.)
Thus, 'moonshot' is evidenced as older than, and "perhaps influenced", the development of 'Moon shot'.
My research, although not airtight, suggests the conceptual heritage of 'moonshot' in the general figurative sense you asked about, as well as 'moon shot' in the playful figurative sense related to the name of a baseball player, derives from the events narrated in De la terre à la lune, or From the Earth to the Moon, an 1865 science fiction novel by Jules Verne first translated and published in English in 1873. In that novel's English translation the specific term 'moonshot' does not appear; however, the novel recounts the adventures of a group of people shot to the moon in a projectile from a gigantic gun, and the phrase 'shot to the moon' appears several times.
Support for the suggestion that the novel is the conceptual ancestor of the sense used in the closed and open compounds appears in the form of figurative uses bridging the dates of (a) the novel's translation and publication and (b) the use of the term in the "spacecraft" sense. The first such use I uncovered was in a column titled "A Genuine Renaissance" from the 19 Jan 1891 issue of the St. Paul Daily Globe.
The nearly twenty year gap between this figurative use and the earlier English publication of From the Earth to the Moon may be explained by the gradual inculcation of the concept in the popular imagination by way of the original work, an opera based on it, a series of translations, and the publication of two (also translated and widely circulated) sequels.
Unfortunately, the use in the St. Paul Daily Globe was the most clearly verifiable linking figurative use I could find. Other uses do appear, but my access to the sources is restricted in ways that leave dates and exact context somewhat muddy.
The other textual links include this explicit collocation, but not the exact term, in an Australian newspaper:
VISITING OTHER PLANETS.
Commander De Pinedo, who a few
months ago made a flight from Italy
to Australia and back, must have been
reading his Jules Verne when he spoke
of the possibilities of inter-planetary
communication. The French author in
his "Voyage to the Moon" shot his
characters from the earth to the moon
in a gigantic hollow shell fitted up as
a room. De Pinedo gives no indication as
to when he thinks man will be
able to travel to other planets, though
he seems to consider Verne's idea feasible.
So much has been done in the
last 30 years that was thought impossible
30 years before that one hesitates
to set bounds to the limit of
human achievement. The greatest
trouble will be to find a way to get
back, supposing that any human being
ever succeeded in landing on another
planet.
(Barrier Miner, Broken Hill, New South Wales, 12 Apr 1926.)
Following the Australian collocation, an appearance in a US journal in 1935 (thus predating the 1949 OED attestation by almost 15 years) uses the precise term with reference to spaceflight, and connects the term explicitly with Jules Verne's novels:
The Jules Verne dream of a rocket to the moon is nearly fulfilled. "But that's old stuff" say the rocketeers. They have more important uses for their rockets right here on earth.
....
The moon shot is pretty much like the piffle of harnessing cosmic rays for fuel. Such pleasant talk obscures the hard engineering fact that we can make rockets do pretty much what we want them to do if we build them large and well enough, ...
(From an article in Column Review: Volumes 1-2, 1935. Combined snippets. Library records indicate the 1935 date is accurate.)
Hindsight of course makes the connection between Jules Verne's From the Earth to the Moon and 'moonshot' seem more compelling than it would otherwise, and in the absence of evidence showing more comprehensive and explicit connections between the science fiction novel in 1877 and the first uses of the term in the early 20th century, my speculation about the origins of the term must remain tentative.