Unfortunately, the rules are potentially different for each country TLD. They are set by the corresponding governments.
There is a wide variation from "anything goes as long as you pay us" to "you must live in this country"(for example).
Most don't have restrictions on the physical location of the hosting server but they often have other restrictions.
Remember that the country where the hosting server is physically located will have legal jurisdiction over the physical server and might have very different laws to the country that controls the TLD you are using.
You should contact your preferred authorised registrar for each TLD you are interested in and ask them what the requirements are.
The rules for the .es TLD are controlled by www.dominios.es.
According to their rules
You can apply for any of the following ".es" domains from any country
in the world as long as you maintain ties to Spain:
Individuals and legal entities and organisations without legal entity
with ties to Spain may apply for any ".es"and/or ".com.es" domain. For
".nom.es" domains, individuals entities with ties to Spain may apply.
Entities, institutions or groups, with or without legal entity, that
are non-profit making and have ties to Spain may register ".org.es"
The ".gob.es" and ".edu.es" domains may be applied for by Public
Bodies and Entities and Institutions related to Education and Research
in Spain, respectively.
The ".gob.es" domains are reserved for Spanish Public Administrations
and the Public Law entities within their sphere. Entities,
institutions and groups, with or without legal entity, that are
officially recognised and undertake activities related to education or
research in Spain may obtain ".edu.es" domains.
YOU CAN REGISTER AN ".ES" DOMAIN FROM ANYWHERE IN THE WORLD, AS LONG
AS YOU HAVE TIES TO SPAIN!