Every student will "touch" the lockers where the student's number, k, is a divisor of the locker number, n. ("Touch" is shorthand for "open if closed and close if open".)
k is a divisor of n if and only if n/k is also a divisor of n. So for the most part, divisors come in pairs. The only exception is when k=n/k, so n is a perfect square.
Where n is not a perfect square, there will be an even number of touches returning the locker to its initial closed state. Where n is a perfect square, there will be an odd number of touches leaving the locker open.
Who wudda thunk that squares had anything to do with it!