Big Question: Can a homunculus take class levels?
If the DM designates the homunculus an ally or a cohort then that homunculus earns enough XP to gain a level then that homunculus can take a level in any class if it meets the class's requirements. Allies "who actually travel with [PCs] on adventures… earn a full share of experience points and treasure just as any other character does" (Dungeon Master's Guide 104). Similarly but more complicated, "[c]ohorts earn experience points, but not at the same rate as player characters" (ibid.). A 3-step procedure described thereafter determines the XP a cohort earns.
A DM can designate a creature an ally or cohort whimsically, but a DM may hesitate to because a homunculus doesn't make decisions. Implicit in the Dungeon Master's Guide on Friends (104–6) is the idea that the less in charge of its own destiny a creature is, the less XP it gains. (Not incidentally, this rather cleverly—if backhandedly—explains why there are so many more level 1 creatures than creatures of level 2 and higher.) Hirelings illustrates this hierarchy by saying
Unlike cohorts, hirelings do not make decisions. They do as they’re told (at least in theory). Thus, even if they go on an adventure with the PCs, they gain no experience and do not affect any calculations involving the party level. Like cohorts, hirelings must be treated fairly well, or they will leave and might even turn against their former employers. (105)
So allies who can do whatever they want and choose to assist you earn full XP. Cohorts who can do whatever they want yet tend to mostly obey you earn a slice of XP. And hirelings (as well as followers—see below) who obey you because you give them a roof, gear, purpose, and/or money earn no XP.
Thus we've circled back to the homunculus: "Homunculi are little more than tools designed to carry out assigned tasks," says the Monster Manual. "They are extensions of their creators, sharing the same alignment and basic nature" (154). They don't disobey. Presumably, if the creator doesn't tell them to do something, they do nothing. And they can't willingly go beyond a certain distance from their creators. (It seems that creators might be able to willingly go beyond a certain distance from them, though.) The DM may rule that a homunculus is only ever a tool, therefore neither a spellbook nor 50 ft. of rope nor a homunculus earns XP.
On the other hand, a homunculus has an Intelligence score and it has a nature even if it's basically the creator's, so I can imagine a PC allowing her homunculus its relative freedom—vowing never to command it and allowing it free reign within 1,500 ft.—just to see what happens. In such a case, I'd be tempted to award the homunculus temporary cohort status: It's denied its liberty, sure, but many decisions can be its own. It can have a destiny of sorts. (And do you have any idea how much trouble it can get into within 1,500 ft.? Imagine a Tiny you that can fly and put people to sleep with a bite!)
As a DM I can see this working as a narrative beat—an individual PC having a few homunculi gaining this way class levels due to being cohorts—, but I can't really see weaponizing this long-term into, like, a Homunculus War College or something that has a significant campaign impact. Such an enterprise's logistics are overwhelming.
Smaller Questions
Can a homunculus gain XP or advance by class level through training?
So far as I'm aware, the game has no mechanics for this. That is, I know of nothing that says A creature can spend X amount of time safely doing Y and this yields [XP/class levels]. The game generally expects encounters to yield XP and XP to lead to class levels. If there were a reasonable way to gain class levels in complete safety I think a lot of folks would do that.
Can a homunculus be designated as a low-level follower then later designated as a higher-level follower so as to garner the homunculus one or more class levels?
Unfortunately, that's not how followers work. The Dungeon Master's Guide on Followers, in part, says
Followers don’t earn experience and thus don’t gain levels. However, when a character with the Leadership feat (see page 106) attains a new level, the player consults the table in the feat description to determine if she has acquired more followers, some of which may be higher level than the existing followers. (You don’t consult the table to see if your cohort gains levels, however, because cohorts earn experience on their own.)
It's true: Your legion never promotes from within. Instead, you just attract more and more higher and higher level outside consultants. I am sure your warriors are totally cool with that, though.
Can an artificer with the feat Mentor take his homunculus as an apprentice?
The game resists this. For instance, Finding an Apprentice says
When a PC takes the Mentor feat, the DM should prepare a small group of three to six prospective apprentices for the character to choose from. At this stage, the prospective apprentices don’t need full statistics; a name, race, class, gender, and one-sentence personality should be enough. When the PC selects one of them, she generates the apprentice’s ability scores, using the elite array (15, 14, 13, 12, 10, 8). The apprentice must have the Apprentice feat, but otherwise the details are up to the player character. A new apprentice starts at 1st level [in a class the mentor has at least one level in], with gear appropriate to his class. (Dungeon Master's Guide II 179 and emphasis mine)
Still, that's what the game says that the DM should do, not what the DM must do. The DM could, instead, totally just allow the artificer to pick as his apprentice his homunculus. The DM must decide if the apprentice homunculus joins the artificer as just a homunculus (with no class levels) or as a homunculus artificer 1. To be clear, the artificer must take the feat Mentor and the homunculus must take the feat Apprentice—somehow. However, the apprentice levels up on the mentor's schedule—mentor gains a level, apprentice gains a level—until the apprentice gains a sixth character level whereupon the apprentice automatically becomes a cohort, which is nifty. Thank the DM.
Can a homunculus take one or more feats or be the subject of one or more spells that will, in turn, earn it XP or gain it class levels?
So far as I'm aware, this question is moot. No feat or spell that I'm aware of grants the creature that takes the feat or that's the spell's subject XP that applies to the creature leveling up. Likewise, I'm unaware of any feat or spell that directly grants the a creature a class level. (However, see the question above about the Mentor feat.)
Can other game elements be marshaled so that a homunculus earns XP or gains class levels?
In some campaigns, an artificer could have his homunculus game level loss (see here) or game lycanthopy (see here) with the help of the 7th-level Clr spell greater humanoid essence [trans] (Races of Eberron 186) that for 1 round/caster level changes a construct's type to humanoid (and the help of, typically, a wight or a weredire eel). I've never heard of a campaign that weaved either idea into its fabric, though. The DM can allow a paladin's special mount to also be a paladin's cohort (DMG 200), but How can an artificer's homunculus also be a paladin's special mount? sounds like an altogether new question. A homunculus that's been the subject of the 4th-level Clr spell imbue with spell ability [trans] (PH 243) that then reads a book of exalted deeds (DMG 277) (minor artifact; 3 lbs.) (or whatever alignment's appropriate for it) should gain a level of cleric, but artifacts appear in a campaign on the DM's schedule. So, generally in a way that's mostly player-facing? No.