This is a little tricky. If you get spotted by any of the spotlights, car headlights, or Buzz's flashlight, you have to save and exit back to the map and try again.
Avoid Buzz! He can turn around randomly and spot you if you are in his area. He also checks on top of tables, so you aren't safe there if he is around.
You first unlock Jessie. Play as her first to rescue Rex, as this is the hardest portion. The timing on getting past the cars at the first "gate" is very specific. You are going to have to get the pattern down and run through when both lights are off of the entrance area. From there, climb up on the darts, and wall jump (carefully) to the top. Flip the switch to the right, then jump under the spotlight as you work your way to the left. Now, the worst part: You have to jump very quickly over the first spotlight and without hesitation continue to jump the last gap before the second light sees you hanging. Drop down, to Rex, then switch to Woody. Keep him for the rest of the level.
The second gate has simpler timing than the first, but you might have to jump at the last second to avoid the beams. When you are using Woody's special jump from hook to hook, don't wait for the prompt. In fact, I just mashed X to get through it. Climb up and to the right. Now, we have us a timing puzzle. First, wait until the first three go out. Run to the third spot, which will be the only safe place. Then, when it opens, run back to spot #2. You should be able to make it to the switch after that transition. The lights will be off on your return trip.
The worst is over. Switch to Buzz just to verify that he isn't in your way (over near Jessie would be ideal) and make your way to the wall near Hamm. All you have to do is avoid the helicopter spotlights as you climb the wall toward the switch, which is quite easy. You have to make sure that you nail the jump over the moving spotlight, though, because that will obviously end it.
Level's over after you open Hamm's cage. Exhale.