The glass shattered because water expands as it solidifies. To go in depth, water is a polar molecule, meaning it has a charge to it. When it's warm enough to be a liquid, this charge doesn't account too much in terms of water molecule interaction. However, when water is cooled and begins to solidify, these polar charges are allowed to intact with each other more readily. Eventually the charges overcome the random motion of the liquid molecule and the molecules form into a solid lattice. This lattice has more space in between the molecules than the liquid form of water, causing its expansion. Water also freezes top to bottom, outside-in. This means that the water in the cup would have not been able to expand upward to relive the excess pressure cause by it's freezing, only out. Because the pressure of the ice expansion exerted a force greater than what the glass could resist, it shattered.