It's pretty simple really:
- when the initial ball came in Barrios was clearly offside
- because he jumped for the header with the Spanish defender it is offside because he is directly becoming part of the play, it doesn't matter what happens afterwards
If Barrios had left it then it would have been a legitimate goal, but then the ball would probably not have fell to Valdez, because a Spanish player would have had a good chance to head it clear.
So it was a good call from the linesman, even though Paraguay deserve to be leading.