The answer to this is that such images are not taken continuously. The HST did not stare at one part of the sky for 10 continuous days, but rather it stared at one part of the sky for short periods over a long time which, when added up, amounted to 10 total days of observations. To quote Wikipedia on the subject of how the Hubble Ultra Deep Field (HUDF) image was taken:
The observations were done in two sessions, from September 23 to October 28, 2003, and December 4, 2003, to January 15, 2004. The total exposure time is just under 1 million seconds, from 400 orbits, with a typical exposure time of 1200 seconds. In total, 800 ACS exposures were taken over the course of 11.3 days, 2 every orbit, and NICMOS observed for 4.5 days. All the individual ACS exposures were processed and combined by Anton Koekemoer into a single set of scientifically useful images, each with a total exposure time ranging from 134,900 seconds to 347,100 seconds. To observe the whole sky to the same sensitivity, the HST would need to observe continuously for a million years.
In the above, ACS is the Advanced Camera for Surveys and NICMOS is the Near-Infrared Camera and Multi-Object Spectrometer.
That, I think, is the basic answer to your question, but there's a few interesting and related points I'd like to point out. First, the HUDF image was actually comprised of many individual images rather than a single 10-day exposure image (as the quote indicates). This would necessarily have to be the case, even if Hubble had the ability to stare at the HUDF region of the sky for 10 days without interruption. Cameras work by taking in light. The Charge-Coupled Device (CCD) which actually collects the light (and converts it to an electric charge) can only hold so much charge before it becomes saturated. At some point, you have to "stop recording" and read out the CCD which clears the accumulated charge. That way, you can resume again with a fresh slate. A 10-day continuous exposure would have easily saturated Hubble's CCD. So even if Hubble did spend 10 continuous days observing this patch of the sky, it would have been broken into many, many smaller exposures. This means, even if the Moon did get in the way during part of such a 10-day exposure, they could have just not made any observations during that time.
The other related fact I wanted to point out is that Hubble has something known as the Continuous Viewing Zone (CVZ). This is a region of the sky which is never obscured by the Earth, Moon, Sun, etc. as Hubble orbits. As the name implies, it allows for continuous viewing, without interruption. The Hubble Deep Field (HDF) (the precursor to the HUDF) was observed in this region. However, there are reasons why you wouldn't want to observe in this region such as it being polluted by "Earthshine".