Allah says:

لاَ تَقْولَنَّ لِشَىْءٍ إِنِّى فَاعِلٌ ذلِكَ غَداً – إِلاَّ أَن يَشَآءَ اللَّهُ

And never say of anything, “I shall do such and such thing tomorrow.”
Except (with the saying), “If Allah will!” And remember your Lord when you forget and say: “It may be that my Lord guides me unto a nearer way of truth than this.” [18:23]

The Prophet sallahu alayhi wa salam:

قَالَ سُلَيْمَانُ بْنُ دَاوُدَ ـ عَلَيْهِمَا السَّلاَمُ ـ لأَطُوفَنَّ اللَّيْلَةَ بِمِائَةِ امْرَأَةٍ، تَلِدُ كُلُّ امْرَأَةٍ غُلاَمًا، يُقَاتِلُ فِي سَبِيلِ اللَّهِ، فَقَالَ لَهُ الْمَلَكُ قُلْ إِنْ شَاءَ اللَّهُ‏.‏ فَلَمْ يَقُلْ وَنَسِيَ، فَأَطَافَ بِهِنَّ، وَلَمْ تَلِدْ مِنْهُنَّ إِلاَّ امْرَأَةٌ نِصْفَ إِنْسَانٍ ‏”‏‏.‏ قَالَ النَّبِيُّ صلى الله عليه وسلم ‏”‏ لَوْ قَالَ إِنْ شَاءَ اللَّهُ لَمْ يَحْنَثْ، وَكَانَ أَرْجَى لِحَاجَتِهِ

(The Prophet) Solomon son of (the Prophet) David said, “Tonight I will go round (i.e. have sexual relations with) one hundred women (my wives) everyone of whom will deliver a male child who will fight in Allah’s Cause.” On that an Angel said to him, “Say: ‘If Allah will.’ ” But Solomon did not say it and forgot to say it. Then he had sexual relations with them but none of them delivered any child except one who delivered a half person. The Prophet (ﷺ) said, “If Solomon had said: ‘If Allah will,’ Allah would have fulfilled his (above) desire and that saying would have made him more hopeful.

[Reported al-Bukhari no. 5242]

It should be said immediately after you say that you will do something in the future.

Shaikh Mulla Ali Qaari rahimahullah:

والحديث يدل على أن من أراد أن يعمل عملا يستحب أن يقول عقيب قوله إني أعمل كذا : إن شاء الله تبركا وتيمنا وتسهيلا لذلك العمل

The Hadith is evidence that it is recommended for the one who wants to do an action to say Inshallah immediately after his statement: “I will do such and such” as a form of seeking blessings, as a good omen, and easiness in that action”.

[Mirqaat ul-Miftaah Sharh Mishkaat ul-Misbaah]
Hafidh Ibn Hajr rahimahullah said:

وَاسْتُدِلَّ بِهِ عَلَى جَوَازِ الِاسْتِثْنَاءِ بَعْدَ تَخَلُّلِ الْكَلَامِ الْيَسِيرِ

“it is used as evidence for the permissibility of saying inshallah after a few words coming between it

[Fath ul-Baari]

When does one not have to say Inshallah?

Shaikh Ibn ul-Uthaymeen rahimahullah:

وفي هذا الحديث: دليل على أنه يجوز للإنسان أن يقول: لأفعلن كذا في المستقبل وإن لم يقل: إن شاء الله ولكن يجب أن نعلم الفرق بين شخص يخبر عما في نفسه وشخص يخبر أنه سيفعل يعني يريد الفعل أما الأول فلا بأس أن يقول: سأفعل بدون إن شاء الله لأنه إنما يخبر عما في نفسه وأما الثاني الذي يريد أنه يفعل أي يوقع الفعل فعلا فهذا لا يقل إلا مقيدا بالمشيئة قال تعالى { ولا تقولن لشيء إني فاعل ذلك غدا إلا أن يشاء الله } فهناك فروق بين من يخبر عما في نفسه وبين من يقول إنني سأفعل غدا

“In this Hadith [Saheeh Muslim 2405] is evidence that it is allowed for a person to say: I will do such and such tomorrow even if he does not say inshallah. However, it is a must that we know the difference between a person informing what is in his soul and a person informing that he will do something, that is he wants to do something. As for the first, then there is no harm to say: I will do it in the future without saying inshallah because he is informing what is in his soul. As for the second, the one who wants to do something, that is, for the action to occur, then he does not say it except by restricting it to the will of Allah. The Exalted says:
لاَ تَقْولَنَّ لِشَىْءٍ إِنِّى فَاعِلٌ ذلِكَ غَداً

And never say of anything, “I shall do such and such thing tomorrow.” [18:23]

So there is a difference between the one who informs of what is in his soul and the one who says I will do something tomorrow…”

[Sharh Riyaad us-Saliheen]

And Allah Knows best
Faisal Ibn Abdul Qaadir Ibn Hassan
Abu Sulaymaan