The color of any material is due to the nature of light itself. Daylight, often called white light, is actually a mixture of different colors of light. When light passes through a material, some of the light may be absorbed, while the rest passes through. The part that is not absorbed reaches the eye as white light minus the absorbed colors. A ruby appears red because it absorbs all the other colors of white light (blue, yellow, green, etc.) except red.
The same material can exhibit different colors. For example ruby and sapphire have the same chemical composition (both are corundum) but exhibit different colors. Even the same gemstone can occur in many different colors: sapphires show different shades of blue and pink and “fancy sapphires” exhibit a whole range of other colors from yellow to orange-pink, the latter called “Padparadscha sapphire”.
This difference in color is based on the atomic structure of the stone. Although the different stones formally have the same chemical composition, they are not exactly the same. Every now and then an atom is replaced by a completely different atom (and this could be as few as one in a million atoms). These so called impurities are sufficient to absorb certain colors and leave the other colors unaffected.
For example, beryl, which is colorless in its pure mineral form, becomes emerald with chromium impurities. If you add manganese instead of chromium, beryl becomes pink morganite. With iron, it becomes aquamarine.
Some gemstone treatments make use of the fact that these impurities can be “manipulated”, thus changing the color of the gem.