A harmful companion is one whose influence encourages dishonesty, mockery, gossip, or wrongdoing. الْوَحْدَةُ خَيْرٌ مِنْ جَلِيسِ السُّوءِ counsels protection from such influence, not withdrawal from family and society altogether.
الْجَلِيسُ الصَّالِحُ خَيْرٌ مِنَ الْوَحْدَةِ describes the positive value of company: a good friend reminds us of what is right, supports worthwhile goals, and gently corrects error.
For example, a classmate who helps with study and discourages cheating is a beneficial companion; someone who pressures others to cheat is not.