Im(w) = 0 means that the imaginary part of w is 0; in other words, that w is a real number, or that there are no terms in w with i.
So we have:
w(x^2+2xyi-y^2+1)=x+yi
wx^2+2wxyi-wy^2+w=x+yi
At this point, taking into account that Im(w) = 0, we know that once this has been simplified, the terms 2wxyi and yi will have canceled, since w cannot have any i's anywhere. And they will only cancel if they are equal, given that they're on opposite sides of the equation. So
2wxyi = yi
2wx = 1
w = 1/2x
Basically, you did exactly what you were supposed to do (you set these two terms equal to each other), but you can't just do that; essentially, you got lucky.
Does that make sense? Keep in mind that I'm still only in calculus, and I haven't had any in-depth study of complex numbers myself, so I could very well be wrong. (Hopefully, I'm not, but I embrace the possibility.)