Answered by Sayyidi Habib Umar bin Hafiz (may Allah protect him and benefit us by him)
How do we avoid keeping company with people who do not benefit us without driving them away from Islam?
You must avoid the wrong that they do while showing them good character and giving them good advice and kind words when the opportunity arises. If they are doing something which is forbidden do not partake in it but do not be harsh to them when they are not doing so. Continue to ask Allah to rectify them and guide them. Keep in mind that they may be guided and then intercede for you on the Day of Judgement. The bounty of Allah cannot be limited.