We don't really know. The best speculation is that they are left over from ancient encounters between the planets and wandering asteroids. They might also have been formed at the same time as the planets themselves and are just left over junk that never could get together to become moons because of the planetary tidal forces. Recently, one of the moons of Saturn observed by Voyager seems to have disappeared. All that remains, according to Hubble Space Telescope observations, are two curiously shaped new moonlets. The speculation is that the moon spied by Voyager got hit by some object and shattered into two of more fragments that are now on different orbits around Saturn. If stuff like this can happen in 10 years, think of what kind of mayhem can happen in a billion years to create planetary rings!