As you see in the picture above, when the object (1) is close to the camera, the distance between green and red dots is 400mm. The distance is measured on the image itself and does not account for perspective/depth.
Thus, when the object (2) is further away, the distance between the same dots is 85mm. The lights on the object are always fixed, as is the camera's field of view.
Based on this fact, we can accurately estimate the distance of the object to the camera based on how far apart the dots appear. In the right-most object (3) you can see that, from the perspective of the camera, the green dots appear closer together than even object (2).
If we only had the two green lights, it would therefore appear to be further away than object (1).However, if you observe the red dots in object (3) being further apart than object (1), we can correctly deduce that it is in fact closer to camera but rotated on its own axis.
In the case of the Oculus Touch controllers, the large donut on top has many infrared LEDs for the Quest headset to track in relation to itself. You cannot see them with the naked eye, but below you can see an image that shows both normal and infrared spectrum colors.