'Cancelling' the common factors from line (4) to (5) means dividing by the factor (X-Y). Since X=Y, this is a division by 0, the results of which are undefined.
(1) X = Y Given (2) X2 = XY Multiply both sides by X (3) X2 - Y2 = XY - Y2 Subtract Y2 from both sides (4) (X + Y)(X - Y) = Y(X - Y) Factor both sides (5) (X + Y) = Y Cancel out common factors (6) Y + Y = Y Substitute in from line (1) (7) 2Y = Y Collect the Y's (8) 2 = 1 Divide both sides by Y