A star is awarded whenever you get a score of at least 500 in a level, which usually means having:
- a clear time of under 32 seconds
- all donuts collected
- a continuous, max-inflow stream of jelly being piped in during the 4 seconds between clearing the level and the results screen
Having an earlier clear time gives you some leeway on the 3rd criteria, but generally the scoring is supposed to be pretty strict for the trickier levels.
It's a challenge system I put in place since I personally enjoyed figuring out strategies and routes for these levels. For world 1 - level 5, my strategy is to wait and let the jelly pool up at the bowl-like peanut butter formation to buffer enough jelly for continuous feeding, and then I'd try to dig in a way so that the jelly doesn't fly above the last stream to avoid bunching it up in the air.
As for the automatic cutoff system, it's there to prevent having to wait too long before pulling up the results, but I'm definitely thinking about some possibilities to make it slightly more forgiving for the scoring.