Praise be to Allah, and peace and blessings be upon the Messenger of Allah. To proceed:
There is no contradiction between these verses, because each verse refers to a different situation.
Allah says:
“Indeed, Allah does not forgive associating partners with Him, but He forgives anything less than that for whom He wills.”
[An-Nisa: 48]
This refers to the person who dies upon shirk without repenting from it. Allah will not forgive such a person.
As for someone who falls into shirk and then sincerely repents to Allah before death, Allah forgives him. Allah says:
“Say to those who disbelieve that if they cease, what has previously occurred will be forgiven for them.”
[Al-Anfal: 38]
Allah says:
“And those who do not invoke another god besides Allah, nor kill the soul which Allah has forbidden except with legal right, nor commit adultery.”
[Al-Furqan: 68]
These are among the gravest sins, and Allah has threatened those who commit them with punishment. He then made an exception for those who repent, saying:
“Except for those who repent, believe, and do righteous deeds. For them Allah will replace their evil deeds with good deeds.”
[Al-Furqan: 70]
Shirk, murder, and adultery share the fact that sincere repentance from them is accepted before death, but the ruling concerning someone who dies upon them differs.
Whoever dies upon major shirk without repentance is among the people of Hell and does not fall under Allah’s will regarding forgiveness.
The Prophet ﷺ said:
“Whoever dies while associating anything with Allah will enter Hell.”
Narrated by Al-Bukhari and Muslim.
As for someone who dies as a Muslim and a monotheist while still guilty of a major sin such as adultery, drinking alcohol, or another major sin, without having repented from it, then he is under the will of Allah. If Allah wills, He may forgive him, and if He wills, He may punish him. However, he will not remain eternally in Hell as the disbelievers do.
This is the meaning of Allah’s statement:
“And He forgives anything less than that for whom He wills.”
The door of repentance is open even for the greatest sins. Whoever sincerely repents before death from shirk, murder, adultery, or any other sin, Allah accepts his repentance.
Allah says:
“Say, ‘O My servants who have transgressed against themselves, do not despair of the mercy of Allah. Indeed, Allah forgives all sins.’”
[Az-Zumar: 53]
It is also stated in a Hadith Qudsi:
“O son of Adam, if you were to come to Me with sins nearly as great as the earth, then meet Me without associating anything with Me, I would come to you with forgiveness nearly as great as it.”
Narrated by At-Tirmidhi.
There is no contradiction between the verses. Rather, there are three different situations:
For this reason, the Prophet ﷺ said:
“Whoever dies without associating anything with Allah will enter Paradise, and whoever dies while associating anything with Allah will enter Hell.”
Narrated by Muslim.
And Allah knows best.