Basically, clouds look gray when they block sunlight. The thicker the cloud, the more light it blocks. They’re thicker when it’s going to rain because there more water vapour in them ready and waiting to be deposited as rain. 🙂
I think it is to do with the density of the water vapour. When it is about to rain, the cloud is totally saturated, so it can’t actually take any more water, and looks darker. That is when it is time to run for cover!