My question is, is it permissible according to the Quran or hadith? In contexts such as prisoners of war, non-believers, sharia law and so forth. We've seen that this action has been done by many terrorist organizations, so is it really haram or halal?
Islam prescribes execution for certain crimes such as murder, apostasy, robbery etc. , see Why do Muslims kill other Muslims?. Further, with certain exceptions like women and children, Islam allows for execution of disbelieving prisoners of war, see Does Quran 8:67 say that prisoners of war should be killed and not ransomed? .
Historically, the standard means of execution in Islam has been beheading. Scholars have even recorded that it is the preferred method of execution, because the Prophet Muhammad ﷺ said:
فإذا قتلتم فأحسنوا القتلة
So when you kill, kill in a good way
This hadith prescribes killing in the best possible manner, which causes the least suffering. Commenting on this hadith, Ibn Rajab said:
وأسهل وجوه قتل الآدمي ضربه بالسيف على العنق
And the least painful means of killing a human is striking at the neck with a sword
Similarly Ibn al-Qayyim wrote:
ضرب العنق بالسيف أحسن القتلات
Striking the neck with a sword is the best of the ways of execution
Further, it has been mentioned at least literally as the means of killing in Quranic verses and ahadith such as:
فاضربوا فوق الأعناق
so strike [them] upon the necks
strike [their] necks
اذهب فاضرب عنقه
Go and strike his neck
فأمرني أن أضرب عنقه وآخذ ماله
(the Prophet) has commanded me to strike his neck and seize his wealth.