Need Help with Piezo Buzzer and Ultrasonic Sensor code

distance is the value that is measured EACH time trough the loop() this can be update thousands of times in a second.
lastDistance is the value you measured in a PRIOR loop (in a past ).
lastDistance is only update to the new value if there has been SIGNIFICANT movement.

What you are doing is comparing the current distance to a previous distance and, only if, they are different are you going to turn on the speaker. If they are the same (within a range+-) the speaker is quiet.

So in essence, the speaker will only beep if, A SIGNIFICANT CHANGE HAS BEEN DETECTED.
hysteresis is the variable that defines HOW MUCH you have to move towards OR away from the sensor before this can be called significant.