This is not something that is within player control, so it's up to the DM.
DMs can build monsters and NPCs however they want, generally speaking. However, player characters abide by a much more limited set of rules.
Allowing non-player characters and monsters to take class levels is something only the DM can do - so this is entirely up to DM discretion. The Dungeon Master's Guide provides rules for giving class levels to monsters on p. 283, under "Monsters with Classes":
You can use the rules in chapter 3 of the Player’s Handbook to give
class levels to a monster. For example, you can turn an ordinary
werewolf into a werewolf with four levels of the barbarian class (such
a monster would be expressed as “Werewolf, 4th-level barbarian”).
Start with the monster’s stat block. The monster gains all the class
features for every class level you add, with the following exceptions:
- The monster doesn’t gain the starting equipment of the added class.
- For each class level you add, the monster gains one Hit Die of its
normal type (based on its size), ignoring the class’s Hit Die
- The monster’s proficiency bonus is based on its challenge rating, not
its class levels.
Once you finish adding class levels to a monster, feel free to tweak
its ability scores as you see fit (for example, raising the monster’s
Intelligence score so that the monster is a more effective wizard),
and make whatever other adjustments are needed. You’ll need to
recalculate its challenge rating as though you had designed the
monster from scratch.
Depending on the monster and the number of class levels you add to it,
its challenge rating might change very little or increase
dramatically. For example, a werewolf that gains four barbarian levels
is a much greater threat than it was before. In contrast, the hit
points, spells, and other class features that an ancient red dragon
gains from five levels of wizard don’t increase its challenge rating.
The DM can use these guidelines to give monsters/NPCs class levels. Jeremy Crawford even suggested this as a possibility in response to complaints about the Beast Master ranger's animal companion's perceived weakness:
Want your D&D character to have a pet/companion? Here’s a little secret: you don’t need special rules for this. Through roleplaying and ability checks (most likely Animal Handling or Persuasion), you can have a buddy, as long as your DM is OK adding a creature to the group.
I wish that was the default presented rules/guidelines in the book, much as anyway can pick up any weapon, and that the Beastmaster ranger wouldn't get a creature by class feature, but rather just better able to fight along with creatures that anyone could have
The default assumption in the D&D rules is that you can befriend people and critters you meet on your adventures. If a class has a special companion (Beast Master, find familiar, find steed, animate dead, etc.), that companion is in addition to creatures you might befriend.
Sure, but that creature's HP won't scale with level, making it hard to bring them with you the whole game. Sure, the DM could grant them extra HP as you all level, but then there's fears of main the Beast Master (partly) redundant.
The “Dungeon Master’s Guide” has guidelines for adding class levels to creatures. Say your wolf friend has been fighting by your side for an adventure, the DM might give the wolf a level in fighter. This is a legitimate use of the DMG rules.
Many people were dissatisfied with the response in that context, but it may address your situation - assuming the DM is okay with it, of course.