When were Muslims ordered to fight in the cause of religion?

Muslims were prepared for war in the way of Allah when the ayat 153–7 of Surah al-Baqarah were revealed to the Prophet (PBUH).
These five ayat indicate that from time to time believers will undergo unusual trials, hardships and misfortunes that will also affect the whole community.
According to Qur’anic teaching, this world is a place of struggle and competition. Life is a chain of never-ending change and transformation, ‘and over time, you will find no alteration in the way Allah treats creation’ Qur’an 35:43. Human beings, like all creatures, face hardship and troubles which disturb and disrupt the patterns of their personal lives. Death, sickness, fear, hunger, grief, poverty and deprivation are only examples of these. Although personal things may be hard to bear, they are not as destructive and frightening as those which sometimes affect the whole community.

Allah tells us:
‘O you who believe, seek assistance through patience and prayer, surely Allah is with those who are patient. And do not speak of those who are slain in Allah’s way as dead. No, they live, even if you do not perceive that and We will most certainly try you with fear, hunger and loss of property, lives and fruit [of your labours], and give good news to the patient ones, who, when misfortune befalls them, say, ‘Surely we are Allah’s and to Him we shall return’. Those who have the blessing and mercy of their Lord, are those who follow the right.’ Qur’an 2:13–157

However, the clear statement regarding fighting was revealed when the polytheists of Makkah pitted themselves against the Prophet (PBUH) with direct attacks on him. This is clearly explained in the Qur’an:

Fight in the way of Allah against those who fight you, and do not exceed the limits. Truly Allah does not love those who exceed limits. Kill them wherever you find them and drive them out as they drove you out, for the effect of their actions to society is more grievous than their death. But do not fight with them at the sacred mosque unless they fight you there, if they do then you may slay them, such is the recompense of the unbelievers. But if they desist, then truly Allah is forgiving, merciful.Qur’an 2:190–194

From the above ayat we may conclude that the word qital means attempting to kill those who are attempting to kill you. This is referred to as in the way of Allah – fi sabil Allah – because its aim is to safeguard lives of the believers. It is an act of worship to please Allah, not to usurp the property of others or to damage their dignity. War approved by Islam is defensive. Its purpose is to protect the rights of humanity. Defence is by its nature a limited enterprise, which is why Allah said soon after, ‘and do not exceed the limit’.