If you look at the code in the first branch of the if, what it is doing is first calculating U by finding a vector in the XZ plane that is perpendicular to N, and then calculating V = N x U. In the second branch it's the same thing but using the YZ plane instead.
You have to have both alternatives because in the first branch if N = (0, 1, 0) then the code will fail (the sqrt will be zero and invlength will be infinite). In the second case it will fail if N = (1, 0, 0). Comparing the X and Y coordinates as it does is probably for numerical stability - it will reduce roundoff error.
There's nothing special about using the XZ and YZ planes - you could write equivalent code using any two of the three axis planes (XY, XZ, and YZ).