From the Super Mario Wiki
Star Crystals are semi-common items in Super Mario Galaxy and Super Mario Galaxy 2 that consist of big, blue Star Bits with strange spiky white crystal structures in them. They appear more often in Super Mario Galaxy 2 than in its predecessor. They are most commonly seen floating in space directly in the path of a Launch Star. When Mario or Luigi fly through the path, they will smash right through the Star Crystal, releasing multiple separate Star Bits that will circle around them and can be collected with the Star Cursor until they land on the next planet.
In Super Mario Galaxy 2, Star Crystals can sometimes be found floating just above the ground, though this is extremely rare, as this will only happen during one level in the Melty Monster Galaxy. In this level, the Star Crystals will be destroyed if they are hit by Rock Mario or Rock Luigi. For some reason, these Star Crystals will only produce four Star Bits when broken. These Star Crystals can also be broken by the Co-Star Luma's spin.
It is also possible to break a Star Crystal using Mario or Luigi's Star Spin. To do this, they must go to Cloudy Court Galaxy, choose the first mission, grab a Cloud Flower, and traverse through until they reach the highest cymbal next to the Launch Star while having one carrying three clouds. At this point, there is a Star Crystal in the Launch Star's path. The player must try to reach the Star Crystal by creating clouds at the peak of their backward somersaults towards the Star Crystal. Even though the jumps may not be high enough, the last backward somersault can reach the Star Crystal. At the peak of the last somersault, the player must Star Spin at the Star Crystal; the Star Crystal will break and Star Bits will follow the player.
Three Star Crystals also appear on the World Map, one near the end of World 1 and two at the beginning of World 2. The player can maneuver Starship Mario into these Star Crystals in order to break them and release the Star Bits inside. These Star Crystals appear to have a yellow spiky crystal inside them, and will release eight Star Bits when broken.