Looking at the Mexican-American War, on paper it seems that Mexico could have done better. Mexico's population at the time was well into the millions (this puts it at over 7 million). The Mexicans had guns and artillery, so there wasn't that big of a technology gap. As a former Spanish colony, they would have also been familiar with European forms of warfare.
Yet, Winfield Scott was able to land at Veracruz, march all the way to Mexico City, and conquer it with fewer than 20,000 men. Mexico promptly sued for peace. Why did Mexico have such a hard time of it?
Don't want to dissuade discussion, but you might find these answers to very similar questions to be of interest:
The first 70-80 years after the independence on Mexico where a chaos in organization and political management (there is a joke in a book of how if you don’t understand Mexican history is not your fault, is the Mexican history fault). During the Mexican American War two of the most important characters on the Mexican side where Antonio Lopez de Santa Anna (a military general and president) and Valentin Gomez Farias (a president) both were liberals (the latter was radical in many aspects) and Freemasons. Santa Anna is considered by many Mexican historians as the worst president Mexico has ever had, he was on and off the power 11 times and constantly changed sides. He was the general during the independence on Texas (where he proved that his golden years as a general were already gone). On the other side Gomez Farias was the first civic ("I do not have a military career" kind of guy) Mexican president, it was not so bad but as I mentioned before he was a radical in liberal thought so he wanted the Church out of the Mexican system but the thing is that the Church was a key pillar in the life of the Mexicans and this caused a lot of trouble in key moments of the war.
Let's go over the point you make to see the big scheme of the things. You mention a population of 7 million, the thing is that a lot of that people were all over the country and a good part of them did not abide by the Mexican government as they had almost none communication because of the distances (the same thing that happened with Texas). When USA attacked and the Mexican government had to retaliate the Mexican government sent Lopez de Santa Anna (you could say that he sent himself along other generals) to combat Zachary Taylor and co. but in the sense of army the Mexican front decided to make a forced levy of men (of almost any kind: able, old, adolescent.) and food. They had a small professional army and a big mob of cannon fodder. The Mexican army did not have that many weapons, a lot of these men in the mob were armed with their own clothes and ranch tools. The thing is that even with all that the Mexican front on that side was winning. Some Zachary Taylor letters to James Polk mention how his men fear the Mexicans as they are fighting as barbaric animals and the tactics of the generals are working to disarray the USA army. Imagine a mad mob of starving people, knowing that they will probably not return home because there is not enough food, running at you with machetes and ranch pikes; all this while the professional army flanked and disrupted the USA forces.
Did I mention they had no food and other resources? Well this was a big factor on the withdrawal of Santa Anna in the battle of Buena Vista. The Mexican army only needed a last push and perhaps they would have won on that front, but Santa Anna decided to return. He had been sending letters to Gomez Farias, that he needed more men and food, but the help and resources arrived until well later. There is a ton of speculation as why Santa Anna made the withdrawal as he was no fool and probably knew he was going to win, making this move quite controversial. Some sources mention that it was indeed the lack of resources, some others mention that the Freemasonry ordered him to return home and so on.
Meanwhile Gomez Farias decided that in order to support the war efforts he "needed" to size large sums of money from the Church. This caused a lot of civil unrest and uprisings against the government. A lot of communities depended on the Church as they provided food, hospitals and all the things the government did not provided. Gomez Farias was able to calm them down by requesting pardon and returning a good part of the money, but the damage was done and the center of the country was not able to make good preparations for the landing of the USA military front that came from the sea. So, Winfield Scott had an easy win pass for almost every battle as a big part of the military went to the north front (Zachary Taylor and co.) and the preparations of defense of the Mexican government were quite bad.
There were a few moments of retaliation from the Mexican government and the Saint Patrick Battalion did help the Mexican war efforts, but all was lost before the mangled main army returned from the north front.
Note: I did make some simplification to a good part of the war efforts on both sides, but I hope you can see a better picture on why the Mexicans where defeated. Sorry if my sources are in spanish but as the other post noted by u/ThreeMoneyAndNoKids the literature in english is scarce compared to spanish literature on the subject.
Sources:
Some responses on here say it has already been discussed/answered. Well i would go a step further and ask: why didnt we just go ahead and grab all of mexico if it was so easy? Was that possibility ever on the table and something thwarted us?