All perfect praise be to Allah, the Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
Befriending these people due to loneliness while hating their sinful actions does not lead to apostasy or hypocrisy. However, it is impermissible for you to remain in their company while they commit such sinful actions. Allah, the Exalted, says (what means): {And it has already come down to you in the Book that when you hear the verses of Allah (recited), they are denied (by them) and ridiculed; so do not sit with them until they enter into another conversation. Indeed, you would then be like them. Indeed, Allah will gather the hypocrites and disbelievers in Hellfire altogether.} [Quran 4:140]
The verse does not necessarily mean that you would be like them in their disbelief or hypocrisy. An-Nasafi commented on the verse, saying, "It means that you would then be like them in the sin that you earn if you remain with them (while they commit the sin). It does not mean that you would be like them in every aspect. When these hypocrites ridicule the Quran, it is considered an act of disbelief; when a Muslim stays with them in such a sinful gathering, he is committing a sin (not an act of disbelief)." There is no harm in sitting with them if they engage in lawful talk.
Your patience with loneliness is better than the distress and misery you might incur if you happen to be influenced by them over time, which is possible. People are influenced by the company that they keep, either positively or negatively. This is evidenced by the hadith narrated by Abu Moosa, may Allah be pleased with him, and in which the Prophet, sallallaahu ‘alayhi wa sallam, said, "The example of a righteous companion and an evil one is that of a person carrying musk and another blowing a pair of bellows. The one who is carrying musk will either give you some perfume as a present, or you will buy some from him, or you will smell its pleasant fragrance, but the one who is blowing a pair of bellows will either burn your clothes or you will smell its bad odor." [Al-Bukhaari and Muslim]
Allah knows best.