How the Spanish and Portuguese colonies of North and South America able to gain independence

How and why were the Spanish and Portugese colonies of North and South America able to gain independence? How was the United States able to build a unified nation? In what ways were the development of the countries of North and South America similar/different?