They film the scene at least twice. Let's say twins A and B are both played by actor C, and there is a body double (same height and weight, same costume and hair available) D.
First time, C is playing A and D is playing B. The camera is positioned (in the old days) so you can't see D's face. C does all the acting. D says lines to give C something to react to and so that small body movements (head and shoulders) match what happens when someone is speaking.
Second time, they switch. C plays B, D plays A. C is facing camera and D is facing away with the face hidden.
Now you intercut C saying an A line with C saying a B line. D is always in the shot providing a back-of-the-head or an arm.
If that's not "cool" enough, you shoot a third time with the actor C positioned to the camera exactly how D was in the scenes. This gives you some side-of-the-face or whatnot to CGI into the scene itself (rather than switching back and forth between frames from the first shot and frames from the second shot as they used to do.)
While this seems like a lot of work, you go through the "shoot it twice" process for pretty much any scene between two people, even if the characters are played by different actors.