How did Games Workshop's oldest Warhammer 40k races - the Aeldari, Necrons, and Orks - come to be? The rapacious Greenskin hordes, haughty remnants of the Eldar empire, and merciless Necron legions are among humanity's deadliest enemies for a reason - they were engineered for conflict. All three species, in different ways, are the product of 40k's oldest, biggest, and most destructive war, a war fought between rival, godlike super-beings over 60 million years before the current, 41st millennium: The War in Heaven.
Like pretty much every part of Warhammer 40k lore, this rabbit hole goes very deep - but I massively dug the War in Heaven when I first read about it, and I think it's some of the most rewarding background reading for any of the Warhammer 40k factions, so I'll tell you the tale as I understand it.
If you're anything like me, you'll get a kick out of knowing that these iconic Xenos races aren't just meaningless beasts (as the ignorant Mon-keigh of the Imperium of Man so often assume) but rather the outcome of tens of millions of years of tumultuous history, which make mankind seem like embryos by comparison.
What few hard facts we know about the War in Heaven are gleaned from snippets in various Warhammer 40k codex books for the Aeldari, Drukhari, and Necron armies, as well as some more obscure lore volumes published by GW over the years. But the picture we can piece together lays the foundation for all the galactic history that followed.
And, appropriately enough for Warhammer, it's a story about very powerful factions trying remarkably hard to kill one another.

The Old Ones, the C'tan, and the Necrontyr
Quite a lot of Warhammer 40,000 lore revolves around who is, or isn't, a god - especially when it comes to the so-called Chaos gods of the Warp.
But, if we take some main criteria for godhood to be predating all other sentient life; being powerful enough to transcend natural laws; and creating and manipulating other sentient life for one's own purposes, then the Old Ones and the C'tan are Warhammer 40k's first, and most influential, gods.
The Old Ones were an ancient, highly intelligent, probably reptilian species of unknown origin. They travelled through our galaxy eons ago, seeded countless planets with new species, and developed advanced technologies that remain functional in the 40k galaxy millennia later, most prominently the Webway. That's right, the Aeldari didn't build their network of safe passageways for interstellar travel without braving the warp - they inherited it from their creators! But we'll get to that.
The C'tan were a very different beast. According to the original 2002 Necrons codex, they began as beings of sentient energy, thought to have evolved during the very early stages of the universe, which became "star vampires", feeding on the raw energies of the still-forming cosmos. They wouldn't take on full, corporeal being until later, when they met a young, desperate, warlike mortal species, and saw in them their own chance for galactic domination.

Which brings us to the third major player in our story: the Necrontyr. In the 41st millennium, we know the Necrons as soulless metal husks, but in the ultra distant past, they were mortal beings - one of the 'young races' that evolved in our galaxy as the Old Ones zipped about, sprinkling planets with life-creating fairy dust.
An extremely short lived species, due to rampant cancers caused by the incredibly inhospitable environment of their irradiated home planet, Necrontyr civilization developed an all consuming obsession with death and entombment, and Necrontyr scientists toiled, above all, to extend their lives. When the Necrontyr encountered the apparently immortal, immensely powerful Old Ones, they wanted the Old Ones to share the secrets of their long (maybe eternal) life. The Old Ones did not oblige.
And so began Warhammer 40,000's first recorded war.

The battle lines form
The earliest stages were, to put things plainly, a walkover. The Necrontyr were fragile, short lived creatures, forced to crawl through space and use stasis crypts to keep themselves alive during interstellar journeys. The Old Ones were gods by comparison, able to use the Webway to hop instantly across the galaxy. There was no competition.
The Necrontyr, soundly defeated, were forced into hiding on the galactic fringe, where they wallowed in their bitter hatred of the Old Ones and plotted revenge. Which is where they met, and in fact named, the C'tan (star gods, in the language of the Necrontyr).
Sure that the C'tan would grant them the power to defeat the Old Ones, the Necrontyr developed technology to let their new 'star gods' manifest as material beings, in bodies made of Necrodermis, a.k.a. living metal. And it worked, but - as we Warhammer 40k fans should all know by now - when you make deals with godlike beings who promise you great power, you rarely get exactly what you were bargaining for.

When the C'tan arrived in their shiny new bodies, their first move was to terrorize the Necrontyr into servitude with overwhelming force. Before long, the C'tan Mephet'ran - now known as the Deceiver - offered the Necrontyr the Faustian bargain that would define their species forever. The Necrontyr could have immortality, and they could have victory over the hated Old Ones, at the C'tan's side - but at the cost of replacing their mortal bodies with metal ones.
Only once this process of 'biotransference' was already well underway did the Necrontyr - and their monarch Szarekh, the Silent King - realize the true cost was far higher. As millions of Necrontyr entered the Soul Forges to become immortal, the C'tan harvested their life forces and feasted on their souls. The metal legions that emerged were no longer Necrontyr; they were the deathless, lifeless Necrons, and they were ready to conquer.

The War in Heaven
With the Necrons diminished in spirit, but vastly empowered in body, and led into battle by incarnate C'tan powerful enough to kill stars and burn multiple planets at will, this new force took the fight back to the Old Ones. This time, it won.
The Necrons and C'tan forced the Old Ones back on every front, devastating them in battle after battle. Countless species that the Old Ones had fostered across the galaxy were exterminated, their life force greedily consumed by the hungry C'tan.
Given that this war took place millennia before the creation of any hard records, we don't know how long the C'tan's galactic reign of terror lasted - but we do know that the Old Ones tried desperately to fight back by doing what they do best: bioengineering dangerous new creatures.

As they continually fell back under the onslaught of the C'tan, Necrons, and an invasion of psychophagic aliens called Enslavers, the Old Ones created a succession of new species intended to help the war effort. They engineered the Aeldari - a species specifically designed to have a strong, fundamental connection to the Warp, in order to make them powerful psykers - and the Krork: an even bigger, stronger precursor form of the green skinned, warmongering brutes humanity would later call Orks.
Though they fought on the losing side in the War in Heaven, these species survived it, enduring long enough to encounter the human race eons later - as did other, lesser known creatures engineered by the Old Ones, including the Jokaero and Hrud. The Old Ones themselves weren't so lucky, and were hunted almost to extinction by the C'tan, Necrons, and Enslavers, effectively vanishing from the galaxy.

But the closing act of the War in Heaven held one last, vital twist. With the Old Ones defeated and nary a mortal race left to consume, the C'tan began fighting and eating each other, in the process weakening themselves, and committing the fatal mistake of giving their attack dog too long a leash.
All through the ages they spent fighting for the C'tan, the ever bitter Necrons had planned revenge against the star gods, for the deception that had cost them their souls. Szarekh, the Silent King, had secretly developed and stockpiled new weapons of unthinkable power, for one purpose: to subjugate the C'tan and claim ownership of the galaxy for himself.

And so the Necrons killed their gods. Some they obliterated completely; others they smashed into countless tiny pieces called Shards, and imprisoned in their dimension-bending Tesseract Vaults, to be deployed later as mere tools on the battlefield. At the end of the War in Heaven, the Necrons stood victorious over a galaxy in ruins, and they saw that there were no more worlds to conquer.
At Szarekh's order, every last Necron, from every dynasty across the galaxy, climbed into stasis crypts and switched to sleep mode for the next 60,000 millennia. What blasted remnants of the galaxy's younger species that were still alive, were left to slowly rebuild from the ashes. And, of these, it was the empire of the psychic Aeldari that rose to rule the galaxy next, constructing their own, very different myths and legends about the War in Heaven.

60 million years later…
In the 41st millennium, almost nobody remains alive that knows the true history of the War in Heaven (and since most of the few who do are Necrons, who aren't really alive, and whose memory banks are almost all severely addled, even that is debatable). It lives on only in a few incredibly rare and ancient artefacts, most of which are in the Aeldari Black Library, or in the hidden museum lair of the eccentric Necron Cryptek, Trazyn the Infinite.
But its repercussions have shaped the Warhammer 40k galaxy in fundamental ways. The Necrons themselves, of course, are now returning to reclaim their dominion, and by definition threaten every other sentient life form in the galaxy. The pylons of Noctilith, or Blackstone, that they invented to seal off the material universe from the Warp, could be the salvation of psychically vulnerable species like humanity, or its downfall.
The Old Ones' creation of the Aeldari - at the time, apparently, a Hail Mary move without much forethought - paved the way for an Eldar empire that dominated the galaxy for most of known history. But it also indirectly brought about the eventual birth of the Chaos god Slaanesh, who was given life by the Eldar empire's tragic fall, and will now forever delight in the temptation, corruption, and destruction of trillions of mortal souls.

Their creation of the Krork - proto-Orks - was much the same. When they bio-engineered, from scratch, a species that reproduces endlessly from almost unkillable spores, and desires nothing more in life than to fight, and nothing else, forever, they probably thought of them as the lesser of two evils, and a problem to solve later (if they ever survived the war). But they didn't, and so the Orks have proliferated ever since, menacing every other species by their very existence.
Even the Emperor of Mankind, when he came along (which was most likely around about the dawn of humanity, so like, 300,000 BCE in real world time) made reckoning with War in Heaven-era technology a key priority. Like the Old Ones and the Necrons before him, he realized that, for humanity to survive and thrive, it needed technology to circumvent, and if necessary suppress, the Warp.
It was his attempt to construct his own version of the Old Ones' Webway that took the Emperor away from his Great Crusade. Thus the legacy of the War in Heaven, and the godlike beings who fought it, helped bring about the disastrous Horus Heresy that doomed mankind to an existence of perpetual war.

So, one way or another, most things that happen in Warhammer 40k lore can be tracked back to its first true war, which is both satisfyingly long-term worldbuilding, and an amusing way to make history rhyme, in a fictional universe that's literally all about people fighting each other.
And now, it's all coming back around. The Necrons are waking up, and, while most of them have either minds degraded to one-track killing, or no minds at all, a few (like Trazyn) still remember the War, and have intimate knowledge of the ancient technologies used in those days.
For the Necrons, who've been in insensate slumber for 60 million years, the War in Heaven was practically yesterday. Now they're back, ready to fight it all over again, and humanity's brightest minds (step forward, Archmagos Dominus Belisarius Cawl) are feverishly trying to reverse engineer tech that was first developed while fully fledged C'tan and Old Ones were still flinging stars at each other.

We 40k fans often wonder where the seemingly endless escalation of the setting's war making can really be going; if everyone's killing everyone all the time, with weapons that are constantly getting stronger, where does it end; what can the, er, grimmer, darker, further future actually look like? The answer is probably to be found in the distant past - and it ain't pretty.
Did I miss something important? Silly question, it's 40k lore, there's always something important that gets left out, however many words you write. Still, come join the free Wargamer Discord community and let me know. We can daydream about how cool Krork models would be together, and theorize about what Cawl's going to do with the pylons, and stuff.