Or because they have to.
The other answers are all correct. In practice, @DaleM is "most correct" because the lion's share of expert witnesses either voluntarily agree to do so because they are paid to do so, or because it is part of their job responsibilities (especially people like medical examiners). But @user6726 is not wrong that public spiritedness is sometimes a factor, and @kaya3 is not wrong that improving one's professional reputation is sometimes a factor.
These answer omit the second most common reason that someone testified as an expert, however, which is that they are compelled to do so by subpoena.
Generally, expert witnesses participate voluntarily and are called "retained expert witnesses" who are hired for a particular case. But, sometimes someone who is primarily a party to a case, or is a non-expert fact witness in a case, also has expertise qualifying that person to be an expert witness, if this expected testimony is disclosed when required under court rules.
A typical example would be a treating physician in a personal injury case. The treating physician testifies as a non-expert fact witness (and may be compelled to do so by subpoena) regarding facts about which the physician has personal knowledge such as the fact that the plaintiff in the tort case was treated by the physician (at the scene of the accident on the way to a kid's soccer game, since the physician was passing by) with heavy bleeding from an artery on his right leg and lacerations all over his exposed skin.
But, once the treating physician is on the stand, the physician can also be qualified and examined as an expert witness, whether the physician wants to do so or not, about the physician's expert opinions regarding the matters upon which the physician render lay testimony. For example, the treating physician might be asked: "in your professional opinion was the plaintiff's leg injury the cause of his inability to walk without a cane today?"
Generally speaking, someone compelled to testify as a non-retained expert is still entitled to compensation as if he were a retained expert, at a court determined rate reflecting the expert witness's usual charges for this work, if this is disputed (in contrast, a purely non-expert witness is entitled only to mileage to the court house and a fee roughly sufficient to pay for lunch in amounts set by statute).
Similarly, a party to a case who has pertinent expertise, may and often does testify as a non-retained expert witness, in addition to testifying about the facts of the case (although testimony about the facts of the case is not required for a party-expert), whether or not the party is subject to a subpoena. In this case, the party is not entitled to compensation for testifying (even if called by another party in the case and not in the party's own case). The compensation/motivation is the same as the compensation for testifying as a lay witness (i.e. the ability to provide truthful information to the court to support their case) and the cost for a party-witness is reduced because the party has to be at the trial anyway in some capacity in most cases.