Once you get to the end of any level (other than the boss stages where your pit against Bowser.), you will notice the flagpole that, when touched, will end the level. If you are able to jump onto the flagpole with either a 1, 3, or 6 anywhere on the timer, then you will get that number of fire works. For example, if you end the level with 233 left on the timer, you will get 3 fireworks, if you get 136, you will get 6 fireworks.