Sounds odd, I know. I think information should be free, but I am also wondering about the self-sustainability of those who offer up their time for free, for SE sites and open source developing. Is the goal attained to spread knowledge, finish a project, etc.?
Journalist George Monbiot reminds us that there are actually four sectors of the economy. Only two of these, business ("private sector") and state ("public sector") receive attention. Actually there are two other neglected sectors; the household (which is taken for granted for reasons addressed by feminist movements) and the commons. The concept of the commons receives so little attention that many people even in the Linux community are not aware of it, but Linux is actually an example of the commons. The commons means resources owned, managed and shared equally by a community. More elaborately:
A commons consists of three elements: an inalienable resource (it cannot be sold or given away), a community that controls and manages it, and the rules and negotiations developed by that community to ensure that the resource is sustained in perpetuity. The wealth arising from its use is shared equally by the members of the community.
The reasons people have for maintaining and contributing to a commons are quite simple; they derive some important benefit from it, and realise that without the participation of people like them, it will not exist. I believe this is a strong motivation, both emotional and rational. Not everyone will feel the need or have the skills/time/means to contribute, but if enough do, the commons will survive and thrive, as Linux has.
Don't tell anyone...
I think I can only speak for myself, and "right" or "wrong" is not applicable here. Although I strongly believe in the reasons @Zanna posted and elaborated, and I'd love to think my single motivation would be to do a "good thing to the world", to me personally, that is a meta thought. I only feel that looking from a distance. An excellent one, but still. Don't get me wrong, I am happy to contribute, and if I feel an answer or solution helped someone, I am thrilled.
There are however many things in life I can do, of which I feel they are useful and helpful to people, but I don't do them. To spend many hours here helping people, we need more than to believe in something or an altruist's mind. Then what is my motivation, on a "primitive" level?
I need to be creative, and I love jumping into issues that seem unsolvable. They are the crossword puzzles I need to feed my mind with. Furthermore, I love coding, and I think I can say I actually learned it for the greater part here and on SO. No clue if it ever will be part of some kind of a "professional" life (in the sense of "paid" professional), but to me, that is irrelevant, I think I got the level. Furthermore, I got many, many other things from hanging around here and contribute. Things I value.
The thought that it helps people is the additional justification for spending so many hours here (or any other community that serves open source activities), and a sweet bonus.
As much as I'd love to say I contribute because it helps the greater good, honestly it comes down to badges and points a lot of the time.
Since you ask about Stack Exchange (SE) sites in general: Many companies are using Stack Overflow Teams as a way to internally help their programmers with knowledge. For example, there are companies using C++ that have complex templating questions to answer and in the process of doing it they use Stack Overflow to find existing answers and there aren't any they add high quality answers that otherwise might not exist.
With regards to Ask Ubuntu in specific, I think a lot of people have had a random question related to Ubuntu at one point and found a great answer here already. This saved them a lot of time and headache. Many experienced Linux users also know that it's very unlikely they know everything about Linux, but the parts they do know they are glad to help out with. It's not uncommon to spend a few minutes, but in the process save someone else hours of their time. It's kind of like carrying the groceries for an elderly person.
Like Mark McDonald said it's also because the points. I'd be lying if I said I didn't like increasing the points and earning the badges. Over time, I think that effect fades, but by the time it's diminished you've already seen the impact you can have and will likely keep doing it.
It's also worth mentioning I actively shame lots of people in the "real world" for not contributing to Stack Exchange sites and in the past if an employer questions my time spent on a SE sites I will simply ask them if they want me to stop using SE sites to resolve questions. Obviously, they don't want me figuring everything out in a bubble so they desire I keep using SE sites, at which point it illustrates the point: How do you think a system of exchanging knowledge can work if everyone only takes and never gives?
My reasons are:
- If I help others, others will help me when I need their help, like karma, and in fact in the early days of stackoverflow, people refrained from answering someone if he doesn't accept answers, correct me if I'm wrong but there was a percentage under the username showing how many accepted answer someone has, and a moderator used to leave a comment asking the user to accept answers. In fact I was blocked from asking on ServerFault and probably still am and the only way for that site to forgive me for asking stupid questions as a teen, is to answer questions, if I give good answers my account will be normal
- A good reputation is great on a CV, I know brilliant programmers who don't have an SE account nor a github account, but a good looking SE account will catch attention when you apply for a job, in fact I'm have a job interview tomorrow and yesterday evening I discovered that I know the CTO, how? Simply because he a top stackoverflow user in my country.
Those reasons are more than enough to guarantee the future for SE, people will always want these things, sites might die, other sites will be created, old people might not be so active, I was much more active 5 years ago, but there will always be new users younger and better than me, who will take my place.
It's already obvious from the other answers that there are multiple modes of motivation for people to contribute on Stack Exchange sites. I can subscribe to some, but also there's one more that was not yet mentioned so far:
I have this drive to document and archive knowledge. In the middle ages, people like me probably worked as monastery librarians, now we work on Stack Exchange :) Genetics might play a role, as I've always been a writer, thinker and hoarder. I just don't like knowledge to be lost, so when I figure out one of my problems I like to put in a bit of extra effort to document it in a format that feels satisfying because it can be useful in the future.
The helpful people were around before Stack Exchange, of course. People would just put the knowledge on their own blogs (so did I), write on mailing lists and newsgroups etc.. Now with Stack Exchange around, we have a nice new format to publish and archive bits and pieces of knowledge. I often do by answering my own questions. And as long as people feel the deal1 is fair, it will continue to be a major "outlet" for knowledge and assistance.
1 Roughly, the deal right now is: Stack Exchange (the company) cares for the hosting and platform development, gets the advertising revenue but lets us use ad blockers. And all our content is licensed under an open license. Sounds good to me …
I try to answer questions and earn points. These points help me to offer bounties on my own questions so that they get answered quickly.
I honestly don't think I have single reason I can point to, Zanna and Jacob answers ring true for me. As far as sustainability is concerned I've been in the business long enough that at this point I'm what my father called semi-retired. I do what interests me, help who I can, and find that I often further my own knowledge in the process. The "unicorn points" are pretty meaningless to me.