The animation below shows the LED’s switching on and off in sequence. The animation is shown in slow motion to make it easy to understand. In reality, this switching happens hundreds of times per second.
So here is the sequence:
Only red LED is on. Sensor measures red plus room light.
Only infrared LED is on. Sensor measures infrared plus room light.
Both LEDs off. Sensor measures only room light.