The thing that saved Apple was the retirement of Classic Mac OS in favor of the NeXT OS (OS X and now MacOS).
In the early 2000s, developers were building for the Internet and there were only two options for hosting: Windows NT and Solaris. The licensing for both was prohibitive (and just annoying) and so Linux/FreeBSD started getting traction on the server side. FreeBSD didn't have the hardware adoption so faded (but is still loved). Linux had enough hardware support to run serious servers but the desktop never was usable (the year of the Linux desktop!).
Having OS X being a UNIX-variant was 'close enough' to develop on Mac (a very usable desktop) and deploy on Linux.
Then, Windows released Vista which was so broken that developers simply had to move to Mac in order to be able to function.
That progression is why Mac went from almost dying to being the most valuable company on Earth.
In the early 2000s, developers were building for the Internet and there were only two options for hosting: Windows NT and Solaris. The licensing for both was prohibitive (and just annoying) and so Linux/FreeBSD started getting traction on the server side. FreeBSD didn't have the hardware adoption so faded (but is still loved). Linux had enough hardware support to run serious servers but the desktop never was usable (the year of the Linux desktop!).
Having OS X being a UNIX-variant was 'close enough' to develop on Mac (a very usable desktop) and deploy on Linux.
Then, Windows released Vista which was so broken that developers simply had to move to Mac in order to be able to function.
That progression is why Mac went from almost dying to being the most valuable company on Earth.