You have to remember how the colonization of the interior of Brazil happened. Most of the time it can be divided in two forms, that aren't mutually excludent:
https://pt.wikipedia.org/wiki/Tabatinga_(Amazonas)#/media/File:Tabatinga.svg
Location of Tabatinga, so you can place it in the map
These settlements usually follow a river, mostly a tributary of the Amazon river basin, as that is how the Portuguese entered the countryside of the Amazon region (by boats). That's also how Brazilians did it until the invetion of the airplane. Heck, most people who live there use boats to move around to this day.
https://pt.wikipedia.org/wiki/Minas_Gerais#/media/File:Minas_Gerais_in_Brazil.svg
Location of the map of Minas Gerais
http://www.niccolomaffeo.es/brasil/mapas/capitales/cuiaba.gif
Cuiabá is the solid pink city in that map
Cuiabá's founding happened this way: bandeirantes fought the Coxipones Natives near there, but were defeated. As they returned to Sao Paulo, they found alluvial gold in a river and the news spread, creating a mini-gold rush to the place. A similar story happened in Vila Boa de Goyaz, Goias first capital. Of course, the Portuguese Crown soon followed the people who were searching for gold, establishing all the necessary structures for proper taxing.
Now that is how most of the settlements were made, but how Portugal got to keep those places is more complicated. To sum it up, Portugal wouldn't be allowed to settle those places in the Treaty of Tordesillas, which initially divided the new discovered places between Portugal and Spain. But, Spain also wouldn't be allowed to settle some places in Asia. The exact line of the Tordesilla treaty in the Western Hemisphere and the Eastern Hemisphere was uncertain. Some conflicts arose out of that uncertaintity until the two countries became one under the Iberian Union (1580-1640). Then the lines became fuzzy and lots of Portuguese/Brazilian settlers took advantage to settle where they shouldn't. At the same time, the Spanish wanted the Moluccan spice trade, so they conquered the Philipinnes. When the Portuguese got their independence back, this question of where exactly each could settle again became a contentious point that led to many wars. Negotiations folowed, the most important treaties being Madrid (1750) and Santo Ildefonso (1777). The most important point of these treaties was the application of principle of "uti possedetis" most of the time, which meant one country would keep what it had occupied. There was also a trade between the Sacramento colony (western Uruguay, settled by Portugal to try to get control of the River Plate) and the Misiones region, which ended up with Brazil (this is not what nowadays is called Misiones in Argentina, but actually Mato Grosso do Sul).
Sources (in Portuguese only, sorry)
ABREU, João Capistrano de. Capítulos de História Colonial, 1907
SIQUEIRA, Elizabeth Madureira. História de Mato Grosso: da ancestralidade aos dias atuais, 2002
Edit: this only explains how Portugal and Spain (and later the former colonies) decided where each territory ended. It must be said that uncertaintity regarding where exactly the frontiers are remained for quite some time, even though a general outline was agreed on. Some of this uncertaintity remains to this day (as happens with almost all countries), but most of them were settled by diplomacy, especially by the important Brazilian diplomat Barão de Rio Branco in the early 20th century, mostly through foreign arbitration.
Also Acre was acquired later in a dispute with Bolivia, and the demarcation of territory between Brazil and the Guyana Basin countries (Guyana, Suriname and French Guyana) is different, as it involved different countries.