Answering questions here in
buddhism.stackexchange.com is often Dhamma teaching, or at least an effort of it. How can that be rightly done?
I think most of the people contributing on Buddhism.SE do not qualify to call themselves Dhamma teachers. Their answers also cannot be considered a Dhamma teaching.
The following is a better approximation, from Mahāgosiṅga Sutta (MN 32):
“Reverend Sāriputta, it’s when two mendicants engage in discussion about the teaching. They question each other and answer each other’s questions without faltering, and their discussion on the teaching flows on. That’s the kind of mendicant who would beautify this park.”
As a side note - a question arose in the comments whether the MN 32 quote should be translated as "about the teaching / Dhamma" (Sujato translation) or "on the higher Dhamma (Abhidhamma)" (Bodhi translation).
The following opinion by Bhikkhu Analayo supports the Sujato translation.
From pages 70 - 71 of the book "The Dawn of Abhidharma" by Bhikkhu Analayo:
In the Mahāgosiṅga-sutta and its Madhyama-āgama parallel the reference to “abhidharma talk” or to discussing the “abhidharma” occurs alongside “Dharma talk” or “teaching the Dharma”. This gives the impression that the two terms Dharma and abhidharma are here interchangeable. In the Mahāgosiṅga-sutta, the prefix abhi- would thus convey the sense of “about” or “concerning” the Dharma. The passage would then describe having a talk “about the Dharma” and discussing “about the Dharma”, abhidharma.49
The notion that the prefix abhi- conveys a sense of superiority appears to reflect a later understanding of the implications of the term. In line with later understanding, the commentary on the Dhammasaṅgaṇī, the Atthasālinī, refers to the present passage in support of the authenticity of the Abhidharma-piṭaka as the Buddha’s word.50
A discourse in the Aṅguttara-nikāya describes a group of elder monks seated together “talking abhidharma talk”.51 In this case the Madhyama-āgama parallel does not employ the term abhidharma at all, but rather describes that these monks were “wanting to settle a matter of dispute, namely to discuss what is Dharma and Vinaya, what is the Buddha’s teaching”.52 This case would be in line with the impression that abhidharma talk can simply stand for talk about the Dharma.
From Udayi Sutta (AN5.159):
"It's not easy to teach the Dhamma to others, Ananda. The Dhamma should be taught to others only when five qualities are established within the person teaching. Which five?
" The Dhamma should be taught with the thought, 'I will speak step-by-step.'
" The Dhamma should be taught with the thought, 'I will speak explaining the sequence [of cause & effect].'
" The Dhamma should be taught with the thought, 'I will speak out of compassion.'
" The Dhamma should be taught with the thought, 'I will speak not for the purpose of material reward.'
" The Dhamma should be taught with the thought, 'I will speak without hurting myself or others.'
"It's not easy to teach the Dhamma to others, Ananda. The Dhamma should be taught to others only when these five qualities are established within the person teaching."
I want to highlight this sutta to everyone who wants to teach Dhamma/Dharma here or elsewhere.
 Aj Thanissaro's "step-by-step" is translate from ānupubbī, meaning "gradually". This means we should not teach what is beyond the ability of the listener. This implies that we should first understand the listener's situation.
 I'm not sure why he translates pariyāyadassāvī as "explaining the sequence". Although pariyāya can mean "sequence", translating it this way makes this point a repetition of the first. Pariyāya has a range of meanings, and in this context I think it means "way" or "habit", as in "mental way/habit" (cetopariyāya). Thus, as a whole, pariyāyadassāvī is "(as) one who sees of the (mental) habit". In modern terms, this point means we need to understand human psychology.
 This point should be plain enough, but let me point out that "compassion" is not translated from karuṇā but from anuddayata, which I prefer to translate as "kindness".
 "For the purpose of material reward" is somewhat loosely translated from āmisantaro but I'm happy enough with it. Although in SE you wouldn't get any physical reward, the reputation points and badges are enough to be considered "material" (as opposed to "spiritual"). Are you greedy for that?
 The last point suggest that we should check what we're actually doing as we try to teach. "Hurting" in general means decreasing our good qualities, increasing the defilements, strengthening the ego, thus ultimately increasing suffering. So, in the name of "teaching Dhamma", are we hurting ourselves? Or hurting others? Instead of helping them, are we causing them to be greedier, feel small, or in any other way increase their defilements and suffering?
Please reflect on this.
Answering questions here in buddhism.stackexchange.com is often Dhamma teaching, or at least an effort of it. How can that be rightly done?
Not necessary. It's Dhamma "teaching" only if one's confident that s/he's gained mastery of the Dhamma both in theory and in practice. Short of any of those factors and it's more like Dhamma "sharing", where one shares suttas/comys. references and any related backup literatures in the hope that they can address the inquiry raised by the OP. And so, by sharing, just providing personal opinion is simply not sufficient. Always follow up with supporting evidence from the suttas/comys/related literatures, etc.