In written English (mainly online) I often come across sentences ending with a question or an exclamation mark with a space before it. Is it always just an error or a typo? Or there are cases when it is a correct English, for example after closing parentheses or some other punctuation marks?
In English, it is always an error. There should be no space between a sentence and its ending punctuation, whether that's a period, a question mark, or an exclamation mark. There should also be no space before a colon, semicolon, or comma. The only ending punctuation mark that sometimes needs to be preceded by a space is a dash.
I see this error most often with people who never really learned to type. In handwriting, spacing is more, um, negotiable and subject to interpretation.
People have mentioned in the comments that, yes, in the past, a small (non-breaking) space was inserted before an ! and a ? These must never start a new line. The space is also a small space, very clearly much more than the space between letters of a word, but much less than a sentence-ending space.
See, for example, this:
From an 1899 edition of Ralph T. H. Griffith's The Texts of The White Yajurveda. (Link)
This is by no means current practice, as Marthaª's answer explains and I suppose the answer to the question 'is it ever correct ...' is no. But was it ever correct? Yes, very much so.
The Chicago Manual of Style recommends a nonbreakable space before and after an ellipsis when the intention is to trail off a sentence.
13.52 Ellipses with other punctuation. Placement of the other punctuation depends on whether the omission precedes or follows the mark; when the omission precedes it, a nonbreakable space should be used between the ellipsis and the mark of punctuation to prevent the mark from continuing over to the beginning of a new line.
This is a specific instance, and Chicago is one of two accepted style guides for (most) American publishing.
"You stop right there! When I get a hold of you, I'll ... !"
"Do you want to, you know ... ?"
There is so much to love about fudge ... . [I feel the ellipsis before a period is the weakest example because it's arguable whether the ellipsis should come before or after the period, if at all.]
A nonbreakable space is still a space.
When you end a sentence with a link
In modern usage, especially online, one time where it is appropriate to put a space is when you are ending the sentence with a link https://ux.stackexchange.com/questions/15226/should-a-sentence-ending-with-a-url-terminate-with-a-period .
Stack Exchange's parser is pretty good (because of how you use Markup) but on many other sites, especially where the text editor automatically linkifies for you put a space in can be the different between your link working or not.
This is a minor exception though, and really you should probably restructure your sentence so it doesn't end in a link, leaving @Marthaa with the more correct answer.
The best, perhaps only, reason for one space between end of sentence and its punctuation is for the !, following upright fonts resembling too closely the ! itself. Only other occurrence would be typographical error. Readability trumps convention.
Written language convention mutates. Consider Webster. Allow brief, supporting narrative: I'm a journalist and an English teacher who now abides my students using ONE space between manuscript sentences--not the conventional, PROPER, two. I don't expect generations born into texting, IMs, emails, all things digital, to embrace tradition. With discussion and exposure, on-line users might accept an intentional, rare space before the !. Now, I'll go read "tips on writing great answers." No, we don't always read manuals before assembly . . . .