Preferably permanently and without losing class abilities.
I tried searching for this immunity, through races, feats and backgrounds, but so far only found the moon druid's transformation into an elemental, which is not ideal.
There are no character abilities or magic items that can accomplish, aside from available features that can transform you into one of the many monsters with immunity to Unconscious via any of the various means of doing so. You’ve observed that a Circle of the Moon druid can do it with Elemental Wild Shape, with true polymorph and shapechange being methods that immediately come to mind.
A 14th level or higher Zealot Barbarian has Rage Beyond Death:
While you’re raging, having 0 hit points doesn’t knock you unconscious.
A raging zealot has temporary immunity to being knocked unconscious by having 0 hit points. Not exactly what you’re looking for, but it’s the closest character ability.
If you're immune to the Unconscious condition with no other special rules modifying the general rules, then definitionally, being reduced to 0 HP means you die immediately, because the general rules say "When you drop to 0 hit points, you either die outright or fall unconscious". If you literally can't fall unconscious, and you don't have a more specific rule overriding those two options, you're dead, immediately, no death saves, no delay.
This isn't a big deal for monsters, since they typically die immediately at 0 HP anyway, but for a PC, it means you have no margin for error, if you hit 0 HP, you better hope the party can Revivify you, because it's too late for simple healing spells. And they'll probably be pretty annoyed that every time it happens, they have to blow 300 GP and an action to get you back on your feet, rather than just a bonus action Healing Word or the like.
As Thomas notes, the Path of the Zealot Barbarian's Rage Beyond Death provides a limited immunity to unconsciousness caused by reaching 0 HP, and by virtue of being a more specific rule, means you don't die immediately either. And it won't affect you if you're polymorphed (because if you hit 0 HP in that form, you revert to your true form, so the immunity doesn't cause any problems there). But if you find an exploit to acquire a generalized Condition Immunity: Unconscious permanently in some other way without special effects that override the effect on reaching 0 HP, you'll regret it.
None of these currently available can grant you immunity to the Unconscious condition.
Thomas Markov's answer's helpful search returns c. 64 monsters with immunity to the Unconscious condition (somewhat fewer if you count the legacy and non-legacy versions of monsters as a single entry, significantly fewer if you additionally count only those monsters with a CR of 20 or less). Most of them are elementals, but there are also constructs, undead, fiends, and a few oozes and monstrosities. None of them are beasts, so a cheaper transformation like polymorph is not available. Instead, you will need to use something like shapechange or true polymorph.
Shapechange is not a great choice because it may only be cast on yourself, limiting your class options to including at least 17th level in druid or wizard. Further, it has a duration of only 1 hour or concentration. True polymorph is better both in that you can have someone else cast it on you, freeing your class options, and that if the caster concentrates for a full hour, the spell will last until dispelled. However, the real weakness of either of these options for your purposes is that they both end when you are at 0 hp. If you were seeking immunity from Unconsciousness so that you could remain conscious even at 0hp, your monster form granting you immunity will itself be removed when you hit 0 hp. You would also prefer to retain your class abilities, and neither of these spells will let you do that1.
As you note in your question, getting to an elemental form with immunity to the Unconscious condition can be achieved with just 10 levels of Moon Druid and the Wild Shape ability. This has the advantage of still giving you another ten levels in other classes if desired, and even better the wild shape permits you to:
retain the benefit of any features from your class, race, or other source and can use them if the new form is physically capable of doing so.
Depending on your DM's interpretation of 'physically capable', most of your class abilities, for druid and other classes, will still be available with the exception of spellcasting (and you can get that at 18th druid for druid spells).
However, Wild Shape still has the same fatal flaw that shapechange and true polymorph do; you revert to your natural form at 0hp, so if your goal is to stay conscious in combat despite damage, you are not accomplishing this.
If you want the ability to stay conscious in a fight, the 14th level Zealot Barbarian ability Rage Beyond Death gives what no monster transformation can:
While you’re raging, having 0 hit points doesn’t knock you unconscious.
It's not exactly permanent, but by 14th level you are getting five rages a day, which should take you through most adventuring days. And it certainly gives you access to all your class abilities - so long as you don't mind your primary class being Barbarian.
What this doesn't give you is protection from Unconsciousness of the kind not caused by damage, but rather from spells and monster abilities. Since you can rage while in wild shape, it seems like you could cover most situations if you raged while wild shaped as an elemental. Unfortunately you don't have enough levels available to be a Barbarian 14 / Druid 10.
Slightly less coverage is available as Zealot Barbarian with a wish spell; by 14th level you should be able to pay someone to cast wish on you with the benefit of:
You grant up to ten creatures you can see immunity to a single spell or other magical effect for 8 hours.
By granting your Barbarian eight hours of immunity to "magical Unconsciousness"2 you would have at least all of the unconsciousness-inducing spells covered, and at least those of the monster abilities that could be considered magical.
Two answers here, including the currently most-upvoted, present frame-challenges, asking why you would even want immunity to the Unconscious condition. They claim that this would actually be a liability, since if damage took you to 0 hp and you couldn't go unconscious, you would then instantly die. This is incorrect, and is based on taking a PHB quote out of context. Whereas one of the answers supports this claim with:
When you drop to 0 hit points, you either die outright or fall unconscious
this is not the full quote. The PHB Dropping to 0 Hit Points unit has the full quote:
When you drop to 0 hit points, you either die outright or fall unconscious, as explained in the following sections.
The "following sections" then explain that "die outright" is only referring to death from massive damage. 'Death because you can't fall unconscious' is not a thing, as further explained here.
Thus, if you were able to attain immunity to the Unconscious condition, and were taken to 0hp in a fight, you would still be conscious and able to act, giving you the option to remove yourself from combat until you could be healed (or you spontaneously recovered with a 20N on your death save). The only downside would be that you could not be stabilized with a Medicine check, since the prerequisite for that is your unconsciousness.
1 At least, not directly. There is always the "brass dragon trick" in which you use shapechange to transform into a brass dragon and then use the dragon's Change Shape to take on a humanoid form in which you can use your class abilities. I don't have all of the source materials to check every monster with immunity to Unconsciousness, but as far as I know, none of them at CR20 and below have Change Shape.
2 At least, if your DM parses "immunity to a single spell or other magical effect" as "immunity to: (a single spell) or (other magical effect). If instead they parse it as "immunity to a single: (spell) or (other magical effect) and then declare that you can make yourself immune to only one monster's effect but not another's, then the wisdom in spending a wish spell on such a limited benefit is questionable.
Frame challenge: you don't want permanent immunity to all forms / causes of unconsciousness, at least not if your table uses the rules interpretation that What happens when a creature with Condition Immunity to Unconscious drops to 0 HP? argues for - instant death on dropping to 0, no grace period of making death saves. See also @Shadowranger's answer on this question.
If your table rules it as a rage-beyond-death situation (except still dying on 3 failed death saves), that would be different if it were possible to get immunity to unconsciousness in a way that didn't result in reverting forms on reaching 0 HP anyway.
(Kirt commented that instant death on 0 HP isn't well supported by the rules, and points to an answer on Is the Spirit Troll unable to be knocked out unless it doesn't regenerate? as evidence that the other ruling can make sense. It would still be living dangerously, as some enemies will stop attacking when their target drops, especially if there are other targets still up. But if still conscious at 0 HP, attacks won't be auto-crits so only one failed death save.)
Elf or Half-Elf racial:
Fey Ancestry. You have advantage on saving throws against being charmed, and magic can't put you to sleep.
This wouldn't help against non-magical things that put you to sleep like a poison, or the sleep breath of a brass dragon.
Class: 3 levels of Warlock for Pact of the Tome, prerequisite for Aspect of the Moon (eldritch invocation).
Prerequisite: Pact of the Tome feature
You no longer need to sleep and can't be forced to sleep by any means. To gain the benefits of a long rest, you can spend all 8 hours doing light activity, such as reading your Book of Shadows and keeping watch.
This includes non-magic causes of Sleep, but still not other causes of being unconscious.
You can't pick this up with just the Eldritch Adept feat since the invocation has a pre-req, so you need 3 levels of warlock for pact of the tome.
If you're a Zealot barbarian with Rage Beyond Death and want immunity to instakill via Sleep, this is a fairly expensive option in levels and needing Cha 13; barbarians already need Str, Dex, and Con. They can wear armor, and if you make it to high level then rage beyond death makes Con a bit less valuable. Hexblade to use Cha instead of Str might not work; many barbarian features require a melee attack using Str.
You can get other useful stuff from the warlock levels. The obvious patron is Hexblade for Hexblade's Curse, which isn't a spell or concentration so you can use it while raging. Crit on a 19 or 20 is very good for a high-level barbarian with Reckless Attacks and Brutal Critical, and the extra +proficiency damage is nice and scales with overall level. And healing when your curse target dies could pull you out of Rage Beyond Death for free, at the end of the fight it you cursed the main enemy.
Celestial warlock gives you some bonus-action healing that's not a spell, so you can do it while raging. Genie warlock lets you add your proficiency mod to one damage roll for an attack on your turn, and most barbarians are going to spend every turn attacking whenever possible. Other patrons have stuff that scales with warlock level or Cha mod, or non-combat.
Eldritch Blast gives you a ranged option if you're far from the front line when combat starts, before you rage. (Perhaps with Grasp of Hadar to pull them closer.) But your spell attack bonus will be garbage if your Cha modifier is only +1, and barbarians already need 3 other stats.
If you can cast Armor of Agathys before combat, rage makes the 10 temp HP last twice as long against physical attacks. It's not concentration. (With 3 levels of warlock, you're casting it at 2nd level.) IIUC, you need to attack in the same turn you rage or it ends at the end of that turn, so you'd need to cast it sometime before combat starts. Mirror Image (2nd) is also something you can cast pre-combat.
For your 2nd invocation, perhaps Book of Ancient Secrets to get ritual casting for 1st and 2nd level spells. That includes Find Familiar, so you can have a familiar giving you advantage on one attack instead of going reckless. Or Fiendish Vigor for free temp HP; rage makes that twice as valuable against physical damage.
(Gift of the Ever-Living Ones for max dice when healing, including short rest hit dice, would be nice, but requires Pact of the Chain.)
Or there are great utility invocations. And you'd have 4 spells known plus 2 cantrips.