Does income while staying in a foreign country usually only count if you are performing work to earn it?

It’s not so simple.

Broadly, the thrust of the US-Spain DTA is to provide that income is (mostly) taxed in the country where it originates rather than in the country in which the recipient is resident. So a Spanish resident earns rent on a property he owns in the US; that rent is taxable in the US.

But there are many exceptions; if you retire to Spain, your pension income in respect of your former US employment is taxable in Spain (but your social security income is taxable in the US). If you establish a business in Spain — a consultancy, say — that derives income from the US, that income is taxable in Spain. If you’re resident in Spain but derive dividend from stock in a US company, those dividends are taxable in Spain (but the US can impose a withholding tax not exceeding 15%, for which Spain will allow a full credit). Etc, etc.

The application of the treaty largely depends on residence, not citizenship. The treaty has rules for determining whether someone who has links to both the US and Spain is a resident of the US or a resident of Spain… Those rules do not depend on citizenship, except in very marginal cases. In most cases, your US citizenship is irrelevant in determining whether you are a resident of Spain for tax treaty purposes, and vice versa. (The treaty does have a clause, however, that says that if you’re a resident of Spain under the treaty rules, that doesn’t stop the US from taxing you as a citizen.)

Finally, the treaty only applies in relation to US/Spain issues. If you’re a US citizen resident in Spain who derives income from Germany, or a US citizen resident in Germany who derives income from Spain, US/Spain treaty won’t apply in relation to the taxation of the income from the third country.