In general, you'll want to pit when the car becomes difficult to control (from tire wear-off) or when you'll run out of gas. Ideally, you'll want to pit as late as possible as much as possible.
For fixed distance races, however, if you can determine the maximum distance you can travel on a regular pit cycle, you can use it to calculate a pit strategy. For the 300km at Grand Valley, if you know that you can go 25 laps on a single pit stop, then that means you'd need 2 pit stops to complete the 60 lap race. As a bit of strategy, you can then spread those pit stops out evenly, so that you make a pit stop on lap 20 and lap 40 instead. If you can make it from here on one more pit stop, you'll want to make it on lap 40 then.