As it is mentioned in the question, the stars are pulled out with a few minutes left in the game. How early they are pulled depends on the point differential, the coach, and the team's momentum.
In the NBA teams often go on runs where they outscore the other team by a wide margin in just a few minutes. It is not uncommon to see a team outscore their opponents by 20 points in as short as a time span as 6 minutes. So even if a team is down like the Rockets were against the Warriors in the game you mentioned at the beginning of the fourth quarter, they'd still have a chance to win, but it's a slim chance. So for that reason the coaches keep their star players in as long as they feel like they can go on a run and have a chance to win. Or on the other hand if they're up, they'll rest the starters if they know they can win with the bench players.
The momentum of the game also matters. You'll see the star players get pulled out earlier if the point differential is getting larger in the 4th quarter. This is because the team that's down needs to slow down the leading team and start dominating which is harder than if they were already cutting down the point differential.