Why do I get 3 or 4 Legendary gem upgrades?

It's related to both time and deaths. From this guide:

Urshi, the NPC that spawns after killing the Greater Rift Guardian, will be your source of legendary gem upgrades — but only if you finished the GR within the 15 minute timer. As a base, you get three chances to upgrade your gem. You gain an additional chance if you do not die even once inside the GR, and prior to starting the GR you can choose to pay gold to "empower" for yet another gem upgrade chance upon successful completion.

To summarize it :

  • Any GR after time has run out - 0 attempts;
  • Normal GR with 1 or more deaths - 3 attemps;
  • Normal GR without dying - 4 attempts;
  • Empowered GR with 1 or more deaths - 4 attempts;
  • Empowered GR without dying - 5 attempts.

Finished GR allows 3 upgrades, +1 if empowered, +1 if you don't die.