Think of authors order on paper as a game for credit, which in turn influences how scarce limited resources (and in case of academia very limited) distributed. It is positions, founding money, opportunities, ... The distribution of resources are exponential on the other hand: some get top positions in top universities, with prestige, good students, and funding. Most get nothing and leave the field.
Think if contributions of people in the field as a bell curve. There are a few stars, they would be recognized easily and get resources. There a few no-chancers, they would be recognized easily and not get resources. Around the mean you have a lot of concentration where it is not possible to easily distinguish, and side factors came into play.
Paper authorship is one of these. Instead of evaluating and pedicuring judging if someone would really go to contribute significantly, we rely on these place holder metrics.
Some fields try to solve the problem locally and allow to authors to sort out who contributed how much. This in turn leads to some times dishonestly ordering authors, e.g. the most senior person with tenure tried to put their soon to be graduate students as first, etc. Mathematicians and TCS as a subculture feel that they are above this kind of human politics, so they instead try to systematically avoid the problem. However the field is full of human politics, as any other area. It takes other forms.
Generally academic mobility is relatively low. If you are a star, you might get tenure in a good university. Otherwise, if you are contributing more than another person, but they have advantages like being in a more prestigious university, working with more famous people, etc. it is hard to break in. Those who get to decide how to allocate resources often turn into like minded circles who naturally like to only work with people similar or familiar to them. Most don't go to do any fundamental works later.
As a general advice, think about why you are really in the field? If you are one of them people who are naturally driven to it, that is your inner satisfaction comes from skiing the research and talking about it and learning and discovering, then keep that in mind and don't worry too much about these political games. Enjoy doing good research. If your satisfaction comes from being smarter than everyone else, either you really are, and most likely get what you want, or you really are not, and would soon learn that. If a major factor for you is resources, pick a field with more abundant resources, if you are even an average graduate student in an average university, you have the skills that can make you earn more than what top academics earn in the industry, or have impact beyond those of typical academics who spends years improving the epsilon that no one uses or really cares about (most academic papers even at top constructed are of that category). And so on.
No system is perfect or immune to human politics. Instead of worrying about the system being flawed (which one is not). Don't expect the system to be fair and give appropriate recognition. Often results are children of their time, despite our huge egos, most of us know that for almost all results, if it was not us, someone else would proven it, maybe a few years later. Even Godel and Turing's famous results were children of their time, similar ideas were present in the works of other people at the time. Cold war was a great social experiment in science where the west and Soviets independently proved major results roughly at the same time.
So check with yourself what you really care about and focus on that and don't worry too much about things like these. If you find yourself worrying too much about fairness if distribution of resources in academia, know that there are much better paths to get resources than academia, particularly anarchy fields like TCS and math.