The Holy Spirit is Salvation. If we have the Holy Spirit, we have Salvation. If we die having the Holy Spirit, we go to Heaven. But we can lose the Holy Spirit by serious and willful sins. There are many examples of this in the Gospels and elsewhere.
Take the Kings of Israel: Saul, David and Solomon. Did they lose the Holy Spirit at any point in their lives. Saul almost certainly did. David did when he sinned but then received Him back and was saved. Solomon too. Otherwise, the Holy Spirit would not have inspired King David to write:
"10Create in me a pure heart, O God, and renew a steadfast spirit within me.11Do not cast me from your presence or take your Holy Spirit from me.12Restore to me the joy of your salvation and grant me a willing spirit, to sustain me.13Then I will teach transgressors your ways, and sinners will turn back to you." (Psa 51).
King David confessed and repented of his sin, and he was forgiven as it says in 2 Chron 7:14 as well as 1 Jn 1:9.
In the NT, we see numerous examples, from Christ Himself, of persons committing serious sins, not repenting, and being lost, though they were originally forgiven/justified.
Mat 18:
"32 Then his master, after he had called him, said to him, ‘You wicked servant! I forgave you all that debt because you begged me. 33 Should you not also have had compassion on your fellow servant, just as I had pity on you?’ 34 And his master was angry, and delivered him to the torturers until he should pay all that was due to him.
Here the Master is very clear that He first forgave that servant. He says the servant should have forgiven his brother/neighbor even as he himself was forgiven by God/Christ. But that servant refused to do so. Hence, he was lost.
Luk 12:
42And the Lord said, “Who then is that faithful and wise steward, whom his master will make ruler over his household, to give them their portion of food in due season? 43Blessed is that servant whom his master will find so doing when he comes. 44Truly, I say to you that he will make him ruler over all that he has.
45But if that servant says in his heart, ‘My master is delaying his coming,’ and begins to beat the male and female servants, and to eat and drink and be drunk, 46the master of that servant will come on a day when he is not looking for him, and at an hour when he is not aware, and will cut him in two and appoint him his portion with the unbelievers.
47And that servant who knew his master’s will, and did not prepare himself or do according to his will, shall be beaten with many stripes. 48But he who did not know, yet committed things deserving of stripes, shall be beaten with few. For everyone to whom much is given, from him much will be required; and to whom much has been committed, of him they will ask the more.
Here the Lord says (1) the servant who faithfully perseveres in doing the Master's Will till death/the Lord's Return will receive Salvation, be rewarded for it, made ruler of all things etc. (2) but that same servant if he does not patiently wait for the Lord's Coming, and falls into drunkenness, and abuses the other servants etc, will be punished by the Lord and lose Salvation. The Lord says this servant will now have the same place as unbelievers, i.e. hell. (3) in the final portion, the Lord says, the more we know, by e.g. reading the Word, the more faithfully we are obliged to practice what we know. Those who do not know will be beaten with fewer stripes. But those who do know, but did not obey, more. Let us try to follow the e.g. of the first faithful servant who faithfully obeyed the Lord, not for a single hour, but until his own death, or the Lord's Coming.
God Bless.