I'll explain it line by line.
list details = llGetObjectDetails(avatarOrObject, [ OBJECT_POS ]);
if (llGetListLength(details) < 1)
{
return;
}
vector pos = llList2Vector(details, 0);
That bit gets the position of the object or avatar with the given key. If the object/avatar doesn't exist or isn't present, 'details' will be empty so the function does nothing and returns.
vector localX = llVecNorm(pos-llGetPos());
That gets the vector offset from the prim to the position we got earlier, and normalizes it to get a unit vector in that direction. The x-axis is usually used as the "forward" or "looking-at" direction.
vector localY = llVecNorm(<-localX.y, localX.x, 0.0>); // z x localX
That is just the equivalent of a cross-product. It finds a vector at right angles to both the global z-axis and localX, and it normalizes it again to get another unit vector. The cross product is simple because it is easy to express a cross product with one of the primary axes <1.0, 0.0, 0.0>, <0.0, 1.0, 0.0>, or <0.0, 0.0, 1.0>. You could to an explicit cross product using the % operator instead if you wanted to, but this is (probably) a little more optimal.
vector localZ = localX % localY;
That finds the third axis that makes up our local coordinate frame. It is a simple cross product, using the % operator this time. Since we know localX and localY are at right angles and are both unit vectors, the result is another unit vector and there is no need to normalize it.
rotation rot = llAxes2Rot(localX, localY, localZ);
That function gives a rotation given three unit vectors, all at right angles to each other (a coordinate frame). It is one of the most useful rotation-related functions there is. Learn to love it.
if (llGetStatus(STATUS_PHYSICS))
{
llRotLookAt(rot, 0.2, 1.0);
} else
{
llSetRot(rot);
}
That simply rotates the object to the desired rotation, using either physical movement or non-physical movement, depending on whether the object is physical or not.