Short answer: the buyer must withhold 3% of the sale price and pay it to the Spanish tax office. That 3% is a payment on account of your capital gain, and if the real tax is lower you can claim the difference back.
Because a non-resident seller can take the money, board a plane and never return. The retention secures part of the tax for the Spanish state before that can happen. It is not an extra tax: it is an advance payment.
The buyer applies it, even if the buyer is also a foreign national. It must be paid over on Form 211 within one month of the deed, and a copy given to you as proof.
If the buyer fails to withhold, the property itself becomes liable for the tax. That is why no properly advised buyer agrees to skip this step.
The capital gain is the difference between the transfer value and the acquisition value:
The rate on that gain is 19% for residents of the EU, Iceland, Norway and Liechtenstein, and 24% for everyone else.
You bought in 2010 for €200,000 and paid €18,000 in taxes and costs. You now sell for €320,000 and pay €9,600 in agency commission.
Here the retention falls short. On transactions with a small gain, or a loss, the opposite happens and a refund is due.
By filing Form 210 for the capital gain within four months of the deed, showing a refund position. The tax office then has six months from the end of that period to pay; after that, with late payment interest.
It is essential that the buyer filed Form 211 correctly. If they did not, there is nothing to refund and the process becomes considerably harder.
This is a separate town hall tax on the increase in land value. In a sale the seller pays it, but where the seller is non-resident the buyer becomes substitute taxpayer and answers to the town hall. Agree in writing who bears the cost before signing.
The kitchen you refitted twelve years ago reduces your gain if you have the invoice. Without it, it does not exist. It is the dullest advice we give and the one that saves the most money.