There's some theories about that out there. Mostly about comets and asteroid strikes. Here's another possibility--- solar wind. Why solar wind? Solar wind is composed of gases, some of which, or perhaps most of which, is hydrogen. Now, the Moon itself is not made of cheese, but of oxygen. The majority of the Moon is oxygen.
So, here's what may be happening: the solar wind strikes the lunar surface. The hydrogen combines with the oxygen giving water. Most of this, but not all, escapes. This water is still trapped in the lunar regolith. That's where the story usually ends. But wait a minute.
What if the water vapor doesn't leave the lunar gravitation field? What if a very small atmosphere exists over the lunar surface? What if this atmosphere is made of this same water vapor that escaped from the regolith? What if this water vapor gets deposited continually in the form of ice at the permanently shadowed craters of the Moon? Since it is so cold there, there's no chance for it to escape again.
Seems to me that I read somewhere on the net that the Moon does have a very, very sparse atmosphere. So, what's in it? Water vapor? That water vapor could explain where the water deposits are coming from.
My two cents anyway.