What will be happen to answers given to questions long before the latter put on hold as off-topic? Is it fair to accuse the answerer for problematic behavior if this would happen for many times in series?
You may want to have a look at the help center. In short, the "worst" thing that could happen is that the whole post is deleted, but this won't automatically happen if your answer has a positive score. Users/moderators could manually vote to delete the post, but in my experience this hardly ever happens.
Questions that get closed are so because the community deems them unfit for the site, either because of its content or its form. Answering them encourages users to keep on posting such questions, which is not a desirable behaviour.
I sometimes find myself in your situation: I post an answer to a question that eventually gets closed. It's natural that this happens from time to time, as reasonable people with good intentions can still disagree over matters of substance. What I consider on-topic will not, 100% of the times, agree with what the community considers on-topic. In those cases you may decide to delete your answer, or leave it be. I have done both things in the past.
If you see yourself in this situation very often, though, there may be a problem. In particular, your idea of how this site should work probably does not align with that of the rest of users. In that case, I believe the best thing you could do, both for your own sanity and for the sake of the community's health, is to either try and change your behaviour or to step back. Not doing so will be a waste of time for everyone.
Some users are more vocal than others, and you should not be surprised when someone lets you know they have a problem with your actions, specially when they become repetitive. Needless to say, just because one person has a problem with you does not mean you are the root of the problem (although it does take two to tango; please try and be nice, even when you know you are the one in the right).
In conclusion: in practice, nothing will happen. The question stays closed and your answer will remain. But, in principle, you should ask yourself whether it is a one-time thing, of you are consistently answering questions that should not be answered.
The existing answers do a good job with the general situation, but it's also important to touch on a couple of specific cases.
There are several categories of questions, which include
- homework questions, and particularly the blatant do-my-work-for-me variety,
- non-mainstream physics, and
- junk-food list questions,
among others, which the community consensus classes quite solidly as harmful to the site, through various mechanisms. Unfortunately, the review queues are often not fast enough to close those questions before they accrue at least one answer. For several of those classes, there is also a community consensus that the answers themselves are also harmful to the site, because they encourage the asker, and future visitors that see the thread, to post further off-topic questions.
If a given answer falls in this category, is it fair to point out to the answerer that that behaviour is problematic? Yes, absolutely. (So long as it is done in a civil way and following the SE Code of Conduct, that is.) This can take the form of a short comment, a downvote, or a vote to delete, none of which are (or should be) personal, but strictly about the post.
On a slightly thornier side: if a given user repeatedly posts answers in that category, even after having been pointed to the relevant resources that point out the community consensus, is it fair to call out that behaviour as problematic? Frankly, I don't see why that's even a question ─ to me it's pretty obvious that it's fair. That doesn't mean that it the repercussions need to go beyond "somebody told me that X aspect of my behaviour is problematic", though (and it doesn't, unless you're really pushing the not-listening-to-the-moderators over into suspension territory).
And, as pointed out by AccidentalFourierTransform, there isn't a clear line that marks this sort of problematic behaviour, and in that blurry boundary there are plenty of cases where reasonable people can (and do) disagree about whether a post is on- or off-topic and about whether a given question is harmful enough to the site that providing answers to it is problematic. However, the blurriness of the line is independent of the fact that we get a steady stream of clear-cut cases of harmful answers to harmful questions, and those do require community action.
Oh, and one more thing, regarding the thread's title:
[...] long before the latter put on hold as off-topic
Unfortunately, community closures often take longer than we would like them to, whether this be by hours, days or months. The fact that a given question slipped through the cracks and, for some reason or another, didn't get picked up by the community moderation mechanisms, has absolutely no bearing on any of the arguments laid out above.
Usually it's no problem if you answer a question and the question later gets put on hold (a.k.a. closed). After all, when a question gets closed, it just means that five people (or one moderator) thought that the question in its current form didn't deserve to get answered. But you might disagree, and think that the question does deserve to get answered. And when you come across a question that you think is good enough to deserve an answer, you can definitely go ahead and answer it. It would be silly to make you wait for everyone else to look at the question and come to a consensus on whether it deserves an answer or not.
However, you should at least be thinking about whether questions deserve answers before you answer them. When you look at a question, take a moment to consider whether it meets the expectations for our site: is it on topic? clearly written? specific? and so on. If not, don't answer it. Flag it for closure instead.
If we notice that a user's judgment about which questions deserve answers is wildly different from the rest of the community, that could get the user a talking-to by the moderators, and if it continued, it could be justification for some disciplinary action. For example, if someone often answers questions which get closed, and many of those questions are very obvious candidates for closure (very far off topic, highly convoluted, or so on), the moderators might want to intervene. In any case I can imagine, this would involve at least one conversation about the norms of the site, and at least one explicit warning (which could be part of the conversation but probably would be separate), and the user would have to be deliberately ignoring those communications, before it escalated to suspension.