As it is often the case, the OP is confusing administrative residency and tax residency. Generally both concepts go together. If you have a resident authorization you are deemed to live in Spain and, as such, you are taxed as a resident.
However the definition of someone that is taxed as a resident (tax residency) is someone who spends in Spain more than 183 days in a calendar year. This is further detailed in Tax Treaties that include tie breakers in case of someone that meets the definition to be taxed as a resident in both countries, that of nationality and that of residence. I won't go into those here in order to keep matters simple, but a tax specialist should be consulted in those cases.
If a foreign person that does not have an administrative authorization to live in Spain does, however, spend in Spain more than 183 days in a calendar year, then he will be taxed in Spain as a resident for that year, unless he can prove he is taxed as a resident in his country of nationality. The fact that he was not authorized to stay in Spain for that long is irrelevant for tax purposes.
I hope this helps to clarify the difference between administrative residency, tax residency and factual residency.