Kiernan's interviews and commentaries demonstrate that she is very astute, precocious, and mature for her age. Certainly, she could simply have been told that Roger and Marie were engaged in a sexual act and that would have been enough to give her to inform her acting. When I was 12 (granted, that was in 1980, so a different time), I knew plenty from a theoretical standpoint, having read novels with fairly explicit sex in them, but it's not like I was telling my parents about that.
Keep in mind, though, that Kiernan filmed "The Chrysanthemum and the Sword", wherein Sally was caught masturbating, when she was 10, so her parents were comfortable enough approaching sexual subjects to go ahead with that.
Do the writers have to be particularly careful about how they script a scene so it can be filmed in a child-sensitive way?
Yes. In the commentary for "The Chrysanthemum and the Sword", they explained a bit about this. In the context of that episode, while there was no problem with the scene where Sally is "playing with herself" (as Betty puts it), they had to alter the scene where the friend's mother confronts Betty about it. Originally, it was written so that Sally is there when the mother tells Betty, but apparently, you can't have adults even mention a sexual act explicitly in the presence of a child on camera so they had to change the script. That actually is less about the child actors and filming situation than about the suggestion that children are involved in something sexual around adults, as a decency standard, but here, it's clear from the camera work that the shots of Sally looking in and of Roger and Marie in the act were probably filmed separately.