Both Spanish and Portuguese come from the same language family, Romance languages. When two independent, different languages belong to one language branch, it means that a long time ago, they were one language. It’s something like relatives in the family. When you take a look at your family tree, you can see one person, a grand-grand-grand mother or father, from whom all of you come. He or she had his own children, and that children had their children, and so on until you were born. All those relatives look like you, some of them less, others much more, but the point is that all of you share the same ascendant. The same is with languages. And since these two languages are siblings, many things they share.