The fourth right: responding to an invitation. It is the right of a Muslim over his brother that, when he invites him, he respond. Attending an invitation is legislated by consensus of the scholars—so far as we know—when the host is a Muslim, is not openly committing sin, and the gathering itself is free of a sin that the guest would be unable to remove. Even so, the majority hold that acceptance is only obligatory in the case of a wedding feast: if the bridegroom names a particular person and invites him on the first day, then—provided the above conditions are met—answering the invitation is compulsory.
If the host is a non-Muslim, acceptance is neither obligatory nor recommended unless some clear benefit is hoped for, such as drawing him closer to Islam. In that case there is no harm in attending, for the Prophet ﷺ once accepted the invitation of a Jew who had invited him in Madinah.
If the host is a Muslim who publicly flaunts sin—shaving his beard, smoking in public markets, or engaging in similar prohibitions—then attending is not obligatory. Should attending serve a worthwhile purpose, you may go; otherwise consider the matter. If declining would serve a greater good—so that, seeing himself shunned and his invitation unanswered, he might repent—then do not attend, hoping Allah will guide him. If no benefit is anticipated either way, you are free to accept or to decline.
When an invitation involves an act of munkar (something objectionable to the Law), and a person is able to remove it, he is required to attend for two reasons. The first is to eliminate the munkar itself.