In Tana'ch, the term "am ha'aretz" meant either "the tribal counsel" as we see when Abraham wanted to purchase the Cave of Machpela, he bowed down to the "am ha'aretz". It also meant "the nation" or "people of the land" as we see this term used in Yeshayahu among other places in Tana"ch. In the mishnah and gemarah (Avot, for example), the term "am ha'aretz" means "ignoramus". When and why did it become a derogatory term, when it never was meant that way, originally?
עם הארץ literally means "people of the land" or natives.
In Avraham's time the natives were the Bnei Cheis, but in the time of Ezra, when the Jews returned from Babylonia to the land of Israel, עם הארץ referred to the current natives, many of whom were Jews who were not scrupulous in their observance of mitzvos. In fact, many of them intermarried (Nechemiah 13:23) and did not even speak Hebrew (24).
The Mishnaic period began shortly afterwards and the term עם הארץ continued to be used to distinguish the unlearned from the learned and those meticulous in mitzva observance.
Note: This is simply a conjecture and I have no hard evidence for this theory.
Also see the Wikipedia article on "Am ha'aretz"