Being invited to present a paper or give a keynote is an honour. It shows that the community recognizes your work. Of course, it is also a way to further advertise your work, or to reflect on what you have done. Generally, the audience is larger for invited speakers.
One accepts the invitation if one has time and prestige of the conference matches one's reputation. If you have a small reputation, an invited talk at a workshop is an honour. If you are super-famous, you'll tend to only accept invitations to prestigious events.
The cost may depend on how much money the conference has in its budget, which will often depend on the sponsors. Sometimes the conference will waive the registration fee, often they may provide accommodation also, and sometimes they may even cover airfare. Bigger conferences will be able to cover more of the costs. Small workshops may not be able to cover any of the costs.