This is tricky to answer so please excuse my long resposne. It is not something that can be answered with a short answer.
Israel was chosen for the sake of being a light to the nations, a Kingdom of Priests (Ex 19:6) to minister to the nations in fulfillment of the Covenant oath sworn to Abraham that through Abraham’s seed all the nations would be blessed.
“By myself I have sworn, says the LORD, because you have done this, and have not withheld your son, your only son, 17 I will indeed bless you, and I will multiply your descendants as the stars of heaven and as the sand which is on the seashore. And your descendants shall possess the gate of their enemies, 18 and by your descendants shall all the nations of the earth bless themselves, because you have obeyed my voice.” (Gen 22:16-19)
The Lord brought Israel out from Egypt and his plan was to prepare them for their call to be a light to the nations so that they could convert the nations. Unfortunately, after bringing Israel out into the wilderness all they did was moan and complain about how they were better off in Egypt and that God and Moses brought them out into the wilderness to kill them. They also went back to their Idolatry (Ex 32, Num 25). How can this stubborn and rebellious people possibly convert the nations? What is God to do? Should he just forget Israel and break his Covenant with Abraham? Should he forget about the Nations?
The Lord has to quarantine the Israelites and purge them from all this evil and teach them how to be holy.
In the context of Numbers 15 we read that there is a distinction made between willfully sinning “with a high hand”
30 But the person who does anything with a high hand, whether he is native or a sojourner, reviles the LORD, and that person shall be cut off from among his people. (Num 15:30)
and sinning unwittingly which could be atoned for and forgiven .
.27 “If one person sins unwittingly, he shall offer a female goat a year old for a sin offering. 28 And the priest shall make atonement before the LORD for the person who commits an error, when he sins unwittingly, to make atonement for him; and he shall be forgiven.
Then comes the story of the man gathering sticks which if read out of context can sound innocent enough. But clearly the context shows that we are supposed to read this as an act committed “with a high hand.” Inotherwords the man is doing this out of outright rebellion against the commandments, even though he knows the penalty (Ex 31:14-15).
This kind of evil could infect the camp and if this man is not brought to justice then the Israelites will think that they can get off with this kind of sinning which would stand in the way of God’s plan to bring the nations into his family. There is no choice but to purge this evil from the people of God and stoning was the common practice in those days (Num 8:26).
Having said all this. God does not want to have to have anyone executed. We see straight after this story that God gives the people a way to avoid sinning so that no one will have to die.
38 “Speak to the people of Israel, and bid them to make tassels on the corners of their garments throughout their generations, and to put upon the tassel of each corner a cord of blue; 39 and it shall be to you a tassel to look upon and remember all the commandments of the LORD, to do them, not to follow after your own heart and your own eyes, which you are inclined to go after wantonly. 40 So you shall remember and do all my commandments, and be holy to your God. (Num 15:38-41).
In conclusion, this man knew what he was doing, he knew the penalty and he did it anyway. On the spiritual level, today many of us know the commandments, and many still willfully break them. This is called “mortal sin.” The punishment for which is spiritual death. The lesson we need to learn from this is that our God is HOLY, He loves us, He does not want us to sin, he wants us to be his children and a light to shine in this dark world and to be an example to those who have not yet come to conversion.
I am sorry my answer is so long but this is a question that does need to be thoroughly answered. I only hope my answers helps.
Thanks for reading.