This is a detailed issue of Usool al-Fiqh that may have vast ikhtilaf according to mazhab.
First thing to note is that there is no inherent difference in this matter between the commands of Allah and the commands of the Prophet (SAW). You could equally find places in the Quran where there seems to be a "command" but it is not considered obligatory by some scholars. In fact, you could find grammatical "commands," that may be haram! This may happen when something is said sarcastically for example.
Like Allah says:
And say to those who do not believe, "Work according to your position; indeed, we are working. And wait, indeed, we are waiting." (11:121-122)
But, it is haram for them to continue in their position of disbelief. Rather, they need to repent and believe.
This stems, in fact, from the question of whether a grammatical "command" is in fact a command in reality. In English, this is known as the imperative form.
The issue is that the imperative form is not necessarily for obligation. When you tell someone, "Do this," you could mean it as an obligation like "Pray five times a day" or you could mean it as a recommendation like "Have fun today" or you could mean it as an allowance that is not a recommendation.
All three are found in quick succession in this passage of the Quran:
O you who have believed, when [the adhan] is called for the prayer on the day of Jumu'ah [Friday], then proceed to the remembrance of Allah (obligation) and leave trade. That is better for you, if you only knew.
And when the prayer has been concluded, disperse within the land (allowance; you aren't obligated or recommended to leave the mosque after Jumuah!) and seek from the bounty of Allah (recommendations), and remember Allah often that you may succeed. (62:9-10)
The fact that some of the imperatives in the above verse are not obligations is fairly obvious for people to see. But, what is a systematic method of determining which imperative is an obligation and which is not? This is where Usool Al-Fiqh comes in.
(Note that the following is very low-level, and is not meant to be comprehensive. Also note that there are other ways something may be made obligatory than the imperative form, but I will concentrate on the imperative.)
The basic assumption when you see an imperative is that it is obligatory unless one of the following points shows us it is not obligatory. E.g. "Establish prayer"
If there is an imperative but there is also evidence (maybe from the Quran or Sunnah) proving that it is not obligatory, you may assume this command is a recommendation.
Examples of how one would know it is not obligatory include the Prophet (SAW) not doing it once, explicit statement showing it is a choice, etc.
E.g. "O you who have believed, when you contract a debt for a specified term, write it down [...]" (2:282) then in the next verse Allah says "And if one of you entrusts another, then let him who is entrusted discharge his trust" thus showing that it is allowed for them to trust each other instead of writing. So, it is a recommendation.
If something is commanded after it was prohibited, the general rule is that it is allowed rather than obligatory or recommended.
E.g. It was initially forbidden even in the nights of Ramadan to have relations with your spouse. Then, Allah allowed it saying "It has been made permissible [...] So now, have relations with them and seek that which Allah has decreed for you." (2:187) Since, this command was given after it was prohibited, the purpose of the command is to allow rather than to obligate or recommend.
Another example is the verse mentioned before about the Jumuah. Allah says "disperse within the land" and this is an allowance after being obligated to go to the masjid (which is the same as being prohibited from being elsewhere in the land).
So, when the scholars differ over whether something is recommended or obligatory, their point of difference is mainly about whether there is in fact evidence to move away from the default of obligatory to recommendation.
And Allah knows best.