He doesn't actually have sex with Rider or Sakura. It's a dream that Rider gave him using her magic. Originally she claims it's to help Sakura's chances with Shirou, but Shirou turns it around on her and reveals that she is also attracted to Shirou (and apparently has no problem sharing him with Sakura).
Well, he has sex with Sakura in a different scene, and if Rider is giving him a "dream" it's likely that she is actually having sex with him during that time, like in HF.