As noted in this question, academics should write research articles. In many fields, these are way more important than books. It's original research that counts for getting a PhD position, a post-doc, a tenure-track position, grants, etcetera. Writing a good textbook about an advanced subject is very difficult and very time-consuming. Its advanced nature means it won't sell many copies, so the money can't be much. Then why is anybody writing advanced scientific textbooks at all? What are the incentives?
Another reason to write an advanced text is that you have developed a body of research around a topic, and it's stabilized enough that you can present the material in a structured form. The incentive here is that the distilled understanding helps you understand your field, and it helps others work in the area as well. In addition, a well-written book can get you lots of citations, impact, and recognition (the same kinds of things you get with papers, but possibly even more).
People around me that wrote a textbook almost always do that because they want to share the course material they developed for, say, an introduction to hydrology course. They are proud of the material, and feel that the specific approach their material takes is not yet represented in the current textbooks. So, I think for a lot of people they feel it is a significant addition to their field. Mind that these almost always already have tenure.
In addition, in the German system there is an additional reason to write a book. Writing a book (or monograph) is one option to get your habilitation. This is an additional step to take in addition to a PhD thesis, often written with at least 10 years of experience.
As someone who is working on an advanced textbook, I've had to ask myself that question. It's certainly not for the money (which I expect will be not nearly enough recompense for the time spent working on it). Here are the answers:
- Citations: Not a big factor for me, since I expect citations to my book will displace a lot of citations for my older papers and shorter expository work on the subject, but as other people have noted, for a lot of people it is an important factor. A book that becomes the "standard reference" for a topic can garner huge numbers of citations. (Also true of review articles.)
- Creating a textbook: It can serve as a textbook for a class I have taught before and may well teach again. This is a class for which there is no existing good textbook. In this sense, for a big investment of time now I make my life easier in the future by creating a good resource for students taking my class.
- New research ideas: Writing it presents an opportunity to organize my thoughts about the field, and to go through and find and fill holes -- or just things I think haven't been done the right way -- in the existing literature. In this respect, it is an extension of my existing research program and inspires new research to fill the holes that I find.
- Understanding existing literature: Writing the book also provides a good incentive for me to go back and understand other peoples' results that I think should be included in the book. There's one paper in particular which is important, but very difficult to understand, and I've put off making the effort to figure it out for many years. The book provides an additional motivation to actually do it, and furthermore make it accessible to others as well.
- Teaching new people about my field: This is partially altruism and partially self-interested. The altruism part is probably self-explanatory. The self-interest part is that if I can provide a good introduction to the field, I may convince more people to work on an area I am interested in, and furthermore can get them to think about it in the ways I like.
Just one or two of these reasons would probably not be enough by itself, but the combination makes it, I think, a good use of my time. I've written these answers as they apply to me, but I think the same mix of reasons, with different weightings, apply to other people who write advanced textbooks.
In the Wiki page for Richard Feynman,
Feynman has been called the "Great Explainer". He gained a reputation for taking great care when giving explanations to his students and for making it a moral duty to make the topic accessible. His guiding principle was that, if a topic could not be explained in a freshman lecture, it was not yet fully understood. Feynman gained great pleasure from coming up with such a "freshman-level" explanation.
My undergrad major is math. Among all the mathematicians, the ones I remember most (good and bad) are the authors of the math textbooks I used in college.
As for the more advanced scientific textbooks, I believe that the efforts putting all the research results together and organizing them are no less valuable than the individual articles.
One case is where you already have the material (course notes, material you gathered from organizing a tutorial on a topic, etc): publishing it as a book used to make it widely available, a lot more than photocopies. Now, with the advent of the WWW, printed books might be less relevant for diffusion, however.
Another incentive, in case you don't have the material ready, might be the same as for writing reviews: if your book is successful, it will be highly cited and enhances your status in your field. Also, you can try to imprint your own ideas and vision for the field in the next generation of researchers.
While this SE website mostly attracts people from more technical and scientific fields which progress through research articles, there are whole areas of academia where the first question that comes up to junior faculty is, "So, what is your book about?". In social sciences (at least in the less technical programs) and humanities, you are expected to publish your dissertation work as a book, so by the time you go for tenure, you need to have at least one, better two -- another one based on your more recent work (which, by the way, is called scholarship, rather then research). You can easily tell by looking at the CVs of professors in say sociology whether they are in a research department or scholarship department: the former list papers first on their CVs, while the latter put books first (and may not have any papers at all).
Having said that, in the technical fields, it is often valuable to have the material summarized in one source, sometimes even for your own reference. That's how some of the advanced textbooks are formed. Some books are produced as edited collections of invited papers from a specialized topic conference, and it may have been a part of the grant funding that you, as the conference organizer, promised to the funding agency, so that your results are disseminated as widely as possible to the people who could not make it to the conference. (Other people mentioned other obvious ways for the books to shape up, mostly from lecture notes.)
A lot of books are written on sabbaticals, when top folks move away from the daily routine and can concentrate on what they enjoy most: playing in their dirt, moving around their Greek letters and integral signs around, etc. Publishers often approach high profile people and suggest to send their books to them; even I get these generic emails from Wiley or CRC or Springer from time to time. Of course, for publishers that's their business and a way to generate money; I don't think the authors are compensated even remotely enough for the trouble of writing a book. At my consulting rate, writing a book is an undertaking worth a new BMW X5; if somebody else is paying for that, as is the case with sabbatical professors, then this may be an entirely different business :).
If your own field moves through research papers, then you simply won't have the time to work on any books, unless this has been discussed with your chair and faculty, and everybody unanimously agreed that you are so good and so famous in your subfield as to afford spending a whole year away from the programmatic research writing a stupid book.
There are many possible motivations for writing a textbook. A few textbooks bring in very large amounts of money for their authors. E.g., Jim Stewart, the author of a home-run calculus textbook, used $24 million from the book to build a house that is famous.
Probably the most common reason for writing textbooks is the reason that I've done it: because I thought the books already on the market were crap.