A scenario that already happened many times. "GM X" comes out with this new book on "Defense Y" and this book becomes an instant classic for its lucid, clear explanations of the Defense's main themes, ideas, tactical motifs, etc. etc. Assuming that chess knowledge is learned from books, and that there was no previous such book on "Defense Y", how did "GM X" learn all these ideas, plans, etc? Did he learn by himself, playing and analyzing game after game played with "Defense Y", then got all his conclusions about the opening plans? Is there some secret "oral transmission of knowledge" among strong players, GMs, etc. so that other GMs told him what the plans are? Or else?
This kind of book is usually a work of synthesis. The author looks at the available games and takes notice of the various plans employed. It is a matter of noticing and making notes and then writing up a synopsis. An opening book can be written without prior personal experience with this opening. One can also discover a new plan, either by one's own thinking or by looking at the way an engine plays.
Sometimes ideas are transmitted orally. Bologan's book on the King's Indian Defense is by his own admission written on the basis of the notes he took while being tutored by his guru Lanka.
Adding to user3456's answer.
The GM also uses ideas from other openings with similar positions. The most famous is from a Fischer game. He found the opening trap from a Russian magazine. The trap is similar to the same trap in the Pirc, the Philidor, Ruy Lopez. Alekhine has played the same trap in his games. There are earlier games which have similar attacking techniques.
The GMs also provide the wrong information.
[Title "Uschold-Wallinger corr. 1985"] [StartPly "24"] [FEN ""] 1.e4 e5 2.Bc4 Nf6 3. d4 exd4 4. Nf3 Nxe4 5. Qxd4 Nf6 6. Bg5 Be7 7. Nc3 Nc6 8. Qh4 d6 9. O-O-O Be6 10. Bd3 h6 11. Rhe1 Qd7 12. Bb5 O-O-O? 13. Qa4! Nd5 14. Rxd5 hxg5 15. Nd4
Black's twelfth move was suggested by theory as the best move. Larsen rated the position as unclear and Suetin said it was roughly even. These evaluations were based from positions they have seen before. This game proved that this pictacular position was an exception to their general knowledge.
Note: I'm not getting the title and startply tags to work correctly. Would someone kindly fix this.
Invariably the choice of opening lines is predicated on the GM's experience with the line and having played it for a good amount of his formative career. You'd be a fool to write a book on an opening based on research only. Playing experience has much to do with it and is probably the dominant aspect of it. I would not buy an opening book from someone who plays 1 d4 about the White Side of the Sicilian. But a book on the French written by (the now deceased, God rest his soul) Victor Korchnoi would get some buzz.
GMs are human and sometimes forget or mix up things, but they make errors like that far less than the rest of humankind. Opening preparation can be aimed at being ready for a particular opponent or tournament situation. Sometimes they prepare something for one event and don't get to use it, so it sits on the shelf until another time when it can be used.
It was said that Dutch GM Jan Timman never played his opening preparations, but all the work he had done inspired him to play something good.
A grandmaster often writes a book when he has a "Eureka" moment. The main moves and ideas of a particular opening were known to him and others beforehand, but he has a special theme and emphasis from which to view the opening in a new way. After he publishes the book, others adopt his "lens" and it becomes known as "his" opening.