how do I write code so that an ultrasonic sensor could turn on or off a motor or servo? I know I would have to use an if-then statement, but I don't exactly know how to do that.
Show us your best effort.
The Arduino language reference section on the if statement.
What is your sensor, what are the expected ranges that the sensor will read, and how does the sensor ranges relate to the servo position?
Those are some of the questions you may want to answer.
Just for example, you have a sensor that reads degrees in C. You have a servo that you want to act as a pointer. The servo can rotate from 0 to 180. You'll only need 0 to 100. You can then relate one degree of temperature equals 1 degree of servo position.
Now that you got the relation, you can learn how to read your sensor, then learn how to torque the servo, and then learn how to combine the code.
You can look at the library for the sensor as to how to read it, and you can look at how to do a servo sweep; code widely found on the out there in the somewhen place.
You could refer to some tutorials: