I know “QED” indicates the end of a proof. We also use $\square$ or similar.
How did we start doing it? It’s a very old practice passed down from Greek mathematicians like Euclid over twenty centuries ago.
But what are the advantages of doing so? I’d like an answer that addresses several contexts, ranging from books to papers to even posts here.
I can make guesses and persuade myself one way or another, but I’d like to hear from those who are more experienced.
I'll offer a somewhat different answer that contends we sometimes have a net benefit from such a strategy, but sometimes don't. So when is it beneficial? Well, that's complicated.
Let's contrast two very different ways of explaining how we know something. Example 1:
This one-sentence proof of the fundamental theorem of arithmetic would be easy enough for a reader who knows (1) how to present a proof by induction in terms of hypothetical minimal counterexamples and (2) that primes divide at least one factor of products they divide, which follows from Bézout's lemma. If you're confident your readers can manage this, you might think it'd be a waste of everyone's time to write
Theorem, with name: blah blah blah
Proof: several sentences $\square$
I almost never write solutions on this website in such a format, partly because I fear verbosity can stop a reader seeing the forest for the trees. Even in my PhD thesis, when I could prove something succinctly I simply "reasoned out loud" in sentences that make it seem more like a string of casual observations, where $\square$ doesn't belong (and, if it were used, you'd feel like it was a strange choice when a full stop would do). Sometimes, that's a good way to do it, if only because (I think) it mirrors the way people understand things. People are accustomed to thinking in sentences, not in a particular formatting style exclusive to text. And while this strategy very rarely uses words such as proof or theorem, in many cases it doesn't hurt the rigour and formality of the proof.
(Mind you, I'll admit if I'm trying to defend such a writing style, the lengthy sentence in my example is "pushing it".)
But you asked why we would use such formatting, right? Well, let's look at a few things that can derail the above style:
As I said, I sometimes see a benefit in making a proof seem more like an obvious observation than something that needs formatting carved out of the rest of the document's flow. I said that not doing this can stop one seeing the forest for the trees; one wants to know the "main point" of the proof, the "reason why" a theorem is true. (Mathematicians rarely think of proofs as explanations, but they can be succinct enough to be comparable to the explanations we find elsewhere in life.) However, sometimes you really need to take the trees one at a time, or there are too many to take in the whole forest at once. Sometimes, a proof is even presented as:
No wonder you need an ending signal after all that, before we move onto the next theorem.
Having said that, you could argue some long mathematical publications, be they PhD theses or Andrew Wiles's proof of Fermat's last theorem, are essentially one long proof with a lot of lemmata (which may or may not be explicitly highlighted as such). The end of that proof doesn't have the same rationale for such signposting, precisely because nothing is next.