In most Sonic games, you collect the Chaos Emeralds by completing a Special Stage. These bonus rounds challenge you to grab the gems before time runs out or before you fall off the course. That said, the exact method varies from game to game — which title are you playing? Let me know and I can point you in the right direction.