A common cause of tomatoes rotting before they ripen is blossom end rot. This is caused by calcium deficiency in the plant, but watering, either from nature or from the gardener, is most commonly to blame rather than the lack of calcium in the soil.
In most cases of blossom end rot, a lack of rain or watering causes the soil around the plant to dry out. Once the plant wilts, the gardener waters it. Tomato plants are resilient and often bounce back in this situation. However, damage has already been done to the plant. The roots are unable to absorb calcium from dry soil, and rot appears once the fruit forms.
Too much water can also dilute the calcium in the soil, leading to blossom end rot. In this case, side-dressing the plants with a bone meal can help increase calcium within the soil. Blossom end rot sprays, which can be directly applied on the foliage, can also help.
It's important to remove any fruit with blossom end rot. This fruit will continue growing and using the plant's energy. Removing the fruit allows the plant to use the energy to produce new fruit that will hopefully be free of rot.