The 'split' of Iberia between Portugal and Spain dates backs hundreds of years to the time of the Reconquista. In my answer I will discuss rough timeframes as to not get bogged down in specific dates for each creation. The Reconquista, or Reconquest in English, dates back to the year 711 CE when the Umayyad Caliphate conquered most of Hispania, modern day Iberian Peninsula, pushing out the Visigoths. From that point, until roughly 780 years later in 1492 CE, Christian kingdoms attempted to expand their kingdoms southward throughout Iberia, and drive the non-christian kingdoms out of the European Continent. During the first 100 years of the Reconquista (roughly 711-846 CE) there was a move by the Kingdom of Asturias and the Kingdom of Francia to conquer the northern regions of modern day Spain. Think any province touching the Bay of Biscay, the Pyrenees, or areas of Catalonia. During the next 50 years you have the creation of the Kingdom of Navarre and the County of Barcelona being born out of the land of the Kingdom of Asturias and Francia.
In roughly 960 CE you have the creation of Kingdom of Galicia (same area where the province of Galicia is today) and the Kingdom of León, just to the east of Galicia. Later in that century the Kingdom of France (no longer known as Francia) pulled back direct control of counties in northeastern Iberia while keeping those areas as subjects. Throughout the next century crowns would be interchanged and so you can see the movement of power in certain areas change between the different Christian Kingdoms (and this would hold true during the next 400 years or so). In 1031 CE, the Caliphate of Córdoba, which still controlled a majority of Iberia, descended into a civil war resulting in the fracturing of the Caliphate into a number of independent Muslim taifa, or Kingdoms. This resulted in a resurgence in the Christian Kingdoms to push southward.
Around 1060 CE we see the creation of the Kingdom of Castile and Aragon. In 1056 CE you have the consolidation of Galicia, León, and Castile under the Crown of Castile and then in 1075 CE those same kingdoms fell under the Crown of León. During the next few decades the Crown of León pushed southward into modern day Toledo and they took over many taifas along the way. That is until 1092 CE when the Almoravids invaded from north Africa. By 1112 CE the Almoravids were able to push north and reconquer the southern taifas and drive the Crown of León back. At their height the Almoravids were able to push their territory to modern day Coimbra, Madrid, Barcelona, and the Balearic Islands. In 1141 CE the Kingdom of Portugal was founded and it was already a somewhat de facto state having been under self-rule for several decades. After Portugals creation, the Almoravid Dynasty came to an end making way for the Almohad Caliphate. Around 1164 CE, the Crown of Castile split with the Crown of León into two separate entities. During this time the Crown of Aragon also encapsulates the County of Barcelona.
It is also important to remember that there was infighting between the Christian Kingdoms, particularly during 11th and 12 centuries. In early 1196 CE, King Alfonso IX of León, and King Sancho VII of Navarre, entered into an alliance with the Almohads, and then later invaded Castile in 1196. Due to pressure from Pope Celestine III, Sancho of Navarre dropped out of the war. Then in the beginning of 1197 CE, pursuant to a request from King Sancho I of Portugal, Pope Celestine III declared a crusade against Alfonso IX. At the same time the Pope also released Alfonso's subjects from their responsibilities to their king stating, "the men of his realm shall be absolved from their fidelity and his dominion by authority of the apostolic see." The Kings of Portugal, Castile, and Aragon invaded León. Alfonso IX sued for peace in October 1197 due to the impending destruction of his realm. Eventually León and Castile would be joined in a Union and León ceased to exist in 1230 CE.
Over the next 100 years the Kingdoms of Portugal, Castile, and Aragon would push southwards to drive the Almohads out of Iberia. In 1257 Portugal would push all the way southward to its modern day borders and their borders in Iberia would go unchanged until the Iberian Union (afterwards they would retain this same border). Aragon and Castile would continue to push southward four roughly 200 years until the only non-Christian state left was the Emirate of Granada. Around 1479 CE the crowns of Aragon and Castile were officially united, which then created the Spanish Monarchy (officially established in around 1492 CE). By 1492 CE the Spanish crown ended the Reconquista by absorbing the rest of the Emirate of Granada into its realm. In 1516 CE, the last remaining section of modern day Spain, Navarre, officially became part of Spain. So from then on there was only Spain and Portugal that existed in Iberia. The borders from then on are the same borders we have today. Between 1580–1640 CE there was also a time called the Iberian Union, when Portugal's King died without an heir. The two countries become one and let's just say the Portuguese did not enjoy that experience. Long story short (as I know this is getting incredibly lengthy) Portugal regained independence.
Summary: The Reconquista, and the annexations and unions of crowns/kingdoms, are the reason why Iberia is split between Spain and Portugal today. Their shared border has been almost identical since the 1500s. Hope this helps!