Why does Puerto Rico have on Olympic team when they are part of the United States?

Puerto Rico has a team because it is a political entity which has its own National Olympic Committee. Even though it is not an independent country, and for most purposes it is equivalent to the 50 states and the District of Columbia, it is distinct enough politically and culturally to be able to have its own Olympic team. Guam, American Samoa and the US Virgin Islands.

The two main reasons why those four territories have their own Olympic committees are firstly, because the locals were able to raise the money and develop the infrastructure to have a team and secondly, because the USA's Olympic committee had no objection to this.

Usually, a nation-state has only one Olympic team, but there are other exceptions. Israel & Palestine have separate teams. So do the People's Republic of China, Taiwan ("Chinese Taipei") and Hong Kong. France has several Overseas Departments which have their own teams.