One strategy is to have the targets flash white for one refresh. If the diode sees that white light, then the computer assumes that you've hit a target. To overcome this limitation, many games also take advantage of the fact that there is a signal embedded in the refresh beam that helps keep the picture aligned. Another common approach is to have electron beams paint the screen black in one refresh pass after you squeeze the trigger, and then make the targets flash white in the next. The photodiode in the gun can see the change from dark to light and sense the embedded signals.
Some games use the screen refresh cycle more directly.
When you squeeze the trigger, the electron beam is instructed to repaint the entire screen white for a split second. The computer that controls the game knows exactly where each target is on the screen, and how long it takes for the screen to refresh itself. By keeping track of the amount of time it takes to see the refresh beam, the computer can calculate where the gun was pointed, and decide whether you've hit the target or not. There are variations on this approach that utilize the refresh signal itself to figure out how well you've aimed your gun, too. With game companies scrambling to make their targeting systems better and better, describing how all this works is a bit like trying to hit a high-speed moving target.