Portuguese is just a modern version of Galician (Galego), a Spanish dialect from northwest Spain and northern Portugal. After the expulsion of the Moors invaders in the 8th century, it became Portugal's language. Castilian (Castellano), the root of modern Spanish, is a close sibling to Galician.
Good and fluent speakers of Portuguese, Galician and Castilian can understand each other without too much effort.