I am not sure how to make Spain defeat Portugal's independence efforts and I feel that even if Spain won one war, the Portuguese would try again.
Yeah. The key is avoiding this war. Removing Olivares like miguelrj suggests and having the king chose another "valido" that exerts less pressure on the non-Castillian kingdoms is a very good start. But of course, Portuguese and Catalonian resources were important for the wars of the crown, and it was not possible to press Castilla any further, so something had to give. A long term solution would be to give up on the northern territories like the United Provinces and the French Comte, and focus on the Ultramaritime and Mediterranean policies (after all, when Portugal joined the crown, it was with the approval of most of the Portuguese nobility, who saw in it a chance to improve the security of the Portuguese overseas colonies).
But as to uniting Spain and Portugal, that one is easy. If Portugal remains Spanish and some sort of French succession like the OTL War of Spanish Succession occurs, then any effort to unite Castile and Aragon into one monarchy would undoubtedly also involve Portugal.
Correct. But it's not unthinkable that Portugal could have supported the Borbon candidate, same as Castile (which would make the secession war shorter). In such a case, it's likely that Portugal could have kept their own institutions and laws, as the Borbons had ben adviced initially by the French monarchy, and as it indeed happened with the Basque country and Navarre.
On the other hand, if Portugal had supported the Austrian candidate, it might stall or turn around the result of the war. That could have meant the independence of Aragon and Portugal, or a more federal monarchy for a united peninsule, respectively.