Let's start by the reason for the revelation of the first verse:
We were on the night of Friday staying in the mosque when a person from the Ansar came there and said: If a person finds his woman along with a man, and he speaks about it, you would lash him, and if he kills, you will kill him, and if he keeps quiet he shall have to consume anger. By Allah, I will definitely ask about him from Allah's Messenger (ﷺ). On the following day he came to Allah's Messenger (ﷺ) and asked him thus: If a man were to find with his wife a man and if he were to talk about it, you would lash him; and if he killed, you would kill him, and if he were to keep quiet. he would consume anger, whereupon he (the Holy Prophet) said: Allah, solve (this problem), and he began to supplicate (before Him), and then the verses pertaining to li'an were revealed:
" Those who accuse their wives and have no witnesses except themselves" (xxiv. 6).
The person was then put to test according to these verses in the presence of the people. There came he and his wife in the presence of Allah's Messenger (ﷺ), and they invoked curses (in order to testify their claim). The man swore four times in the name of Allah that he was one of the truthful and then invoked curse for the fifth time saying: Let there be curse of Allah upon him if he were among the liars. Then she began to invoke curse. Allah's Messenger (ﷺ) said to her: just wait (and curse after considering over it), but she refused and invoked curse and when she turned away, he (Allah's Apostle) said: It seems that this woman shall give birth to a curly-haired black child, And so she did gave birth to a curly-haired black child.
(In Sahih Muslim, but also different versions in Sunan abi Dawod1 2 3, Jami' at-Tirmidhi 1 2 3 and Sunan ibn Majah)
here the word punishment - does it mean the punishment of the hereafter or punishment of stoning?
of course the punishment referred to here is that of this life as in the hereafter four witnesses are not needed to come to a verdict and punishment (hadd). Imam ibn Kathir said:
(But she shall avert the punishment)
meaning, the prescribed punishment.
(if she bears witness four times by Allah, that he is telling a lie. And the fifth; should be that the wrath of Allah be upon her if he speaks the truth.)
The wrath of Allah is mentioned specially in the case of the woman, because usually a man would not go to the extent of exposing his wife and accusing her of Zina unless he is telling the truth and has good reason to do this, and she knows that what he is accusing her of is true. So in her case the fifth testimony calls for the wrath of Allah to be upon her, for the one upon whom is the wrath of Allah, is the one who knows the truth yet deviates from it. (Source qtafsir)
Some of the longer narrations (qualified as da'if?) show that the woman who was involved in the situation has chosen not to disgrace her people and seemingly preferred the punishment in the hereafter or had hope in Allah's mercy if she repented:
Then the woman got up and took the oaths, and when she was about to take the fifth one; That the wrath of Allah be upon her if he speaks the truth', the people stopped her and said to her: 'It will definitely bring about Allah's curse upon you (if you are guilty).'" Ibn 'Abbas said 'So she hesitated, and recoiled so much so, that we thought that she would withdraw her denial. But she said: 'I will not dishonor my family for the rest of their days.'
… (See for example in Jami' at-Tirmidhi)
does this mean here that if either the accuser or the one being accused lies in his/her testimony... and if the liar recognizes his faults and wants to change and therefore repents to Allah and He will forgive him.
First be aware that both parties if they have sworn a fifth time have saved themselves from a severe punishment. But certainly one of them might have lied.
This may mean if one of these people when asked to do his fifth oath came to mind and told the truth instead of lying Allah may forgive him (in the hereafter) no matter if he/she will be punished by stoning or being lashed as stated in another sahih hadith which is included in some of the longer versions of the hadith quoted above:
Narrated Ibn 'Abbas:
Hilal bin Umaiya accused his wife before the Prophet (ﷺ) of committing illegal sexual intercourse with Sharik bin Sahma.' The Prophet (ﷺ) said, "Produce a proof, or else you would get the legal punishment (by being lashed) on your back." Hilal said, "O Allah's Messenger (ﷺ)! If anyone of us saw another man over his wife, would he go to search for a proof." The Prophet (ﷺ) went on saying, "Produce a proof or else you would get the legal punishment (by being lashed) on your back." The Prophet (ﷺ) then mentioned the narration of Lian (as in the Holy Book). (Surat-al-Nur: 24) (Sahih al-Bukhari)
But let's look in books of tafssir for a better understanding:
Ibn Kathir says in his tafssir:
(And that Allah is the One Who forgives and accepts repentance,)
means, from His servants, even if that comes after they have sworn a confirmed oath. (Source: qtafsir)
Some scholars like at-Tabari and al-Baghawi say: the meaning of this verse is that Allah's mercy here is that by the procedure of L'ian اللعان he saved both from disgrace by revealing their sins and from a severe punishment this life so one should be thankful and perform repentance hoping Allah might accept it in the hereafter. So the procedure of Li'an even if it separates husband and wife forever it saves both from disgrace and a big harm in this life and it also gives the liar a second chance to repent while he is alive.
Some others like al-Mawardi gave two possible interpretations of Allah's mercy : the first is that Allah gave both the option and time to repent instead of being punished (soon in this life) and the second is that were there not His mercy the liar would have received a severe punishment.