I need more understanding, from what I understand swords were mostly used by nobles because they looked cool and were expensive. But were they really useful in combat. Especially compared to axes and spears? Please explain, keeping the discussion mostly to medieval times.
Axes are more cost efficient don't require much training especially compared to swords and have a lot of utility. For example axes are effective against armor and some axe have spikes which increases there effectiveness against armor. You can hook an opponents blade and disarm them with an axe or hook an appendage or shield. Axes heads are very durable, not sure about the stick part, guess it depends on the material. The sword looks like it really struggled against medieval armor. Axes are about just as sharp and are more blunt than swords. I guess without plated armor you might want a sword over an axe.
Spears are amazing, you can distance your self from the opponent, are easy to use, cost efficient, very quick and agile, apparently were the most common weapon in the whole past, not to mention the pike and halberd were amazing weapons.
Swords were expensive, required lots of training, and have a hard time dealing with the most common weapon in history.
Your premise is correct in that swords have often been relegated to secondary weapons in medieval battle, but this is quite contextual and dependent on the era in question.
In the famous battles of Crecy and Agincourt (as an example), the majority of men-at-arms would be using polearms as their primary weapon. This was either a lance for use on horseback or a variety of other weapons such as poleaxes, halberds, glaives etc. for use on foot. These are effective against armour and have a long reach - making them ideal "battle" weapons.
Secondary weapons may include a sword (for use against lightly armoured opponents) or a dagger. Note that the threat of the dagger was as useful as its effect - heavily armoured men could be wrestled to the ground and the threat of a dagger to the face would quickly compel them to surrender for a profitable ransom.
Moving away from pitched battle (which was actually pretty rare), the types of weapons would change. Carrying a polearm is quite impractical, especially while riding long distances. Most fighting during war was skirmishing or just terrorizing the local population, so heavy armour was less common. This is because it is difficult to carry, uncomfortable to wear for long periods and very difficult (or impossible) to put on without a helper. Lighter (and easier to don) forms of armour such as brigandine was very common, even among the knightly classes, and this might have been worn without significant leg/arm protection. In this context a sword or falchion becomes quite practical - they are light, easy to carry and a good all-rounder in terms of offence/defence against lightly armoured opponents.
Finally, and importantly, swords were used extensively in a civilian context. They would have been a status symbol, a way to show off your masculinity, and a method of self-defence in a society with no police. Again, their practicality is useful here. They can be worn on the waist while walking or riding, are light and they can be drawn quickly if you get into trouble.
So, there are a LOT of assumptions being made in this question, and as always with answers regarding weapons, armor, and combat, the answer tends to be "It depends".
Lets get a couple of things out of the way quickly.
Now, that said, to the meat of your question about how effective swords were against axe and spear in the medieval period?
Well, spears have a distinct disadvantage against a swordsman with a shield that closes past the point. Axes tend to lack hand protection and don't really give any reach advantage over a sword.
Each is a tool for a specific purpose, and depending on what your goal was, you would deploy the correct tool.
Spears and axes also make terrible backup weapons for archers, because archers ALREADY have a giant haft of wood to haul around in the form of their bow.
That said, spears are easy to manufacture and easy to train, and axes are very powerful against shields and heavy armor.
Which one is going to be more effective is REALLY context dependent.
Swords have the advantage of being versatile. A good sword can cut, thrust, parry, and any number of other things a weapon needs to do and do all of them well enough for the most part.
A spear, with some exceptions, is pretty much limited to the thrust. It's also more difficult, though still VERY doable, to wield a spear with a shield.
The axe is powerful, but requires more skill than you might think to wield on a battlefield. And is also vulnerable. You can't use a shield with an axe and you also can't form a wall of axes like you can spears. Your very powerful offensively, but very vulnerable. And for most people, battles were about surviving.
Swords are ALSO a social symbol. More or less of one depending on the time and place. But that tended to have more to do with WHAT sword you had than "Did you have a sword?".
For example, there were medieval german laws that said "Only nobles could carry a sword." Great. So how was a sword defined? Oh it had a peened or threaded pommel construction. Anything else was just a knife.
Enter the Messer. Messer, you may recognize is the german word for knife. And, by medieval german law, they were knives as they had riveted full tang handle scales.
https://armthearmour.tumblr.com/post/179660746444/an-interesting-and-ornate-langes-messer-germany
So here are some examples of historical Messers. As you can see, these are just swords with the serial numbers filed off.
Swords WERE status symbols for many people, but they were ALSO practical tools of personal defense and battlefield use throughout the medieval period, and anyone that COULD get them, DID and used them.
For further reading on this subject, I recommend Ewart Oakshotts books.
You can find them on Amazon at this link.