Snape, in a DADA class, says that a ghost is "the imprint of a departed soul". When Harry asks Nearly-headless Nick if Sirius would come back as a ghost, Nick tells him that everybody has a choice, but that Sirius would not come back. Later, when Harry faces Dumbledore's death, he admits that his beloved headmaster would have "gone on". He himself had that choice "at" King's Cross, after he is hit by Voldemort's Avada Kedavra in the Forbidden Forest.
From here, I see two theories, or possible explanations:
- The soul of a ghost continues "by itself", but the ghost is no longer attached to it. So the soul would face death anyway (or whatever happens to souls), but the ghost, as a separate entity, would not feel it, would not know about it. In contrast to this, people in paintings seem to have some connection to their soul.
- This would imply there is some kind of universe or existence for souls only, which is not elaborated in the books, though it is mentioned a few times.
- As a counterargument, Nick also tells Harry that he chose to stay as ghost because he was afraid of death, which raises the question: what exactly stays, if the ghost is just an imprint of the soul?
- The soul of a ghost ceases to exist, ceases to be. However, the soul has a last chance of taking a "snapshot of itself", and this would be the ghost, an imprint of that soul, an everlasting memory of the person as he or she was at the time of physical death. That's why ghosts keep their personality and woes and don't change them. For example, The Gray Lady is (always) shy, while Nick is sociable and depressed.
However, I don't think we have conclusive information (at least in-universe, which is my only source). Perhaps the author has a clear idea, but I don't know whether she has stated it in public.