Sinning openly in Islam
Question

I heard from our Friday khutbah that a person will not be forgiven if he has bragged and boasted to his friends about his sin while Allah had hidden it, and he quoted a Hadith to support his claim. Is this true? I thought Allah forgives all sins! I am really in despair now.
Praise be to Allah.Does Allah forgive sins?
With regard to the idea that Allah forgives all sins: this is correct, because Allah says (interpretation of the meaning):
{Say: “O My slaves! who have transgressed against themselves (by committing evil deeds and sins)! Despair not of the Mercy of Allah, verily, Allah forgives all sins. Truly, He is Oft-Forgiving, Most Merciful.} [Az-Zumar 39:53]
How to expiate for minor sins in Islam
Minor sins are expiated for by doing acts of worship and by avoiding major sins, because of the following evidence:

Allah says (interpretation of the meaning):

{If you avoid the great sins which you are forbidden to do, We shall expiate from you your (small) sins, and admit you to a Noble Entrance (i.e. Paradise).} [An-Nisa’ 4:31]

It was reported from Ibn Mas`ud (may Allah be pleased with him) that a man kissed a woman, then he came to the Prophet (peace and blessings of Allah be upon him) and told him about it. Then Allah revealed the words (interpretation of the meaning): {And perform As-Salah (establish the prasyer), at the two ends of the day and in some hours of the night [i.e. the five compulsory Salah (prayers)]. Verily, the good deeds remove the evil deeds (i.e. small sins).} [Hood 11:114]. The man said, O Messenger of Allah, is this just for me? He said, “It is for all of my Ummah.” (Narrated by Al-Bukhari, 503 and Muslim, 2763)
It was reported from Abu Hurayrah (may Allah be pleased with him) that the Messenger of Allah (peace and blessings of Allah be upon him) said: “The five prayers and Jumu’ah are an expiation from one week to the next, so long as you do not commit major sins.” (Narrated by Muslim, 233)

There are many things that expiate for minor sins, such as fasting, Qiyam (optional night prayer), Wudu’, etc.
How to expiate for major sins in Islam
But major sins require specific acts of expiation, such as sincere repentance, the carrying out of the Islamic Hadd (a fixed prescribed punishment) on the one who committed them, etc., as can be seen in the following texts:

It was reported from `Ubadah ibn As-Samit (may Allah be pleased with him) that the Messenger of Allah (peace and blessings of Allah be upon him) said, when there was a group of his Companions around him: “Give allegiance to me that you will not associate anything in worship with Allah, not steal, not commit fornication, not kill your children… and not disobey me in what is right. Whoever among you fulfils this, his reward will be with Allah. Whoever commits any of these sins will be punished in this world and that will be an expiation for him. Whoever commits any of these sins but Allah conceals it, then it will be for Allah to decide: if He wills, He will forgive him, and if He wills, He will punish him.” So we gave allegiance to him on that basis.

The phrase “he will be punished in this world” means, the Islamically-prescribed Hadd will be carried out on him.

It was reported from Abu Sa`id Al-Khudri (may Allah be pleased with him) that the Messenger of Allah (peace and blessings of Allah be upon him) said: “Among the Children of Israel there was a man who killed ninety-nine people. Then he went out and came to a monk and asked him, ‘Can I repent?’ He said, ‘No,’ so he killed him. Then he started asking, and a man said to him, ‘Go to such and such a town.’ But death caught up with him, so he inclined his chest towards (that town, as he was dying). The angels of mercy and the angels of punishment disputed concerning him. Allah inspired (the good town) to come closer to him, and (the evil town) to move away from him, then He said, Measure the distance between them. They found that he was closer to (the good town) by a hand-span, so he was forgiven.” (Narrated by Al-Bukhari, 3283 and Muslim, 2766)

What happens if a sinner dies while sinning?
But if a sinner dies in a state of sin, if his sins include Shirk, then Allah will not forgive him in the Hereafter. If his sins are less than that, then the person is subject to the will of Allah – if He wills, He will punish him and if He wills, He will forgive him.
Allah says (interpretation of the meaning): {Verily, Allah forgives not that partners should be set up with Him (in worship), but He forgives except that (anything else) to whom He wills} [An-Nisa’ 4:48, 116].
It was reported that Ibn `Umar (may Allah be pleased with him) said: I heard the Prophet (peace and blessings of Allah be upon him) say: “The believer will be brought close to his Lord until He conceals him and makes him confess his sin. Allah will say, ‘Do you remember such and such a sin?’ The man will say, ‘Yes, I admit it.’ The Lord will say, ‘I concealed it for you in the world and I forgive you for it today.’ Then He will close up his record of good deeds. As for the others or the disbelievers, it will be called out before the witnesses, ‘These are the ones who disbelieved in their Lord. The curse of Allah is upon the wrongdoers.’” (Narrated by Al-Bukhari, 4408 and Muslim, 2768).
Is it permissible to despair of the mercy of Allah?
Your despairing of the mercy of Allah is not permissible, because of the following evidence:

Allah says (interpretation of the meaning): {Say: “O My slaves! who have transgressed against themselves (by committing evil deeds and sins)! Despair not of the Mercy of Allah, verily, Allah forgives all sins. Truly, He is Oft-Forgiving, Most Merciful} [Az-Zumar 39:53]. And He says (interpretation of the meaning): {… and My Mercy embraces all things…} [Al-A`raf 7:156]
It was reported from Abu Musa (may Allah be pleased with him) that the Prophet (peace and blessings of Allah be upon him) said: “Allah spreads out His hand at night to accept the repentance of those who committed sins during the day, and He spreads out His hand during the day to accept the repentance of those who committed sins during the night. (He will continue to do this) until the sun rises from the West.” (Narrated by Muslim, 2759)

Openly sinning in Islam
Speaking about your sin and flaunting it openly in front of your friends is prohibited in Islam, and is a major sin. It is one of the ways of spreading immorality among the Muslims, encouraging evil and tempting others to do similar things. It also means that one does not take sin seriously and regards it as insignificant, and that the sinner is damaging his own reputation and exposing his honour to the slander of others. Islam seeks to put people off from doing such things in the strongest possible terms, as in the following Hadith:
Abu Hurayrah said: I heard the Messenger of Allah (peace and blessings of Allah be upon him) say: “All of my Ummah will be forgiven except those who sin openly. It is a part of sinning openly when a man does something at night, then the following morning when Allah has concealed his sin, he says, `O So and so, I did such and such last night,’ when all night his Lord has concealed him and the next morning he uncovers what Allah had concealed.” (Narrated by Al-Bukhari, 5721 and Muslim, 2990)
On this basis, we advise you to repent sincerely, and if you are tested with any kind of sin, do not speak openly of it or brag about it. You should be aware of the greatness of the One against Whom you are sinning, seek forgiveness for your sin, weep for the error of your ways and adhere strictly to the path of Allah. Beware of despairing of the mercy of Allah. If you adhere properly to the religion of Allah, you will have glad tidings of goodness in this world and in the Hereafter. We ask Allah to help us and you to do that which He loves and is pleased with. May Allah bless our Prophet Muhammad.
And Allah knows best.