They are leaching out of the rock and sand. Anything that died on the rock such as alge, sponges, bacteria are decaying. The decaying matter releases the ammonia as this builds bacteria that are still alive deeper in the rock will start to process the ammonia and turn it into nitrite (NO2). As this builds and the ammonia becomes less the bacteria will start to process the nitrite into nitrate (NO3). You can further break this down through use of oxygen free zones such as deep sand beds or denitrification coils. In absence of oxygen the bacteria will use nirtrate and nitrite combined with sulfate for resperation. However typically people just rely on water changes to keep nitrate in check.
This is where you have to be carefull though. Once the decay stops and the bacteria have used up the available ammonia and nitrites you will get test readings of 0 for both. But a short time after, if you had a really heavy cycle, the bacteria will be without food and start to die off creating a smaller cycle and this will continue untill it reaches an equillibrium. Most the time you really don't have to wait for much past the second stage of the cycle to start adding a clean up crew, and then slowly stocking the tank.