Imaam as-Saa’di (rahimahullaah) said:
Certainly, a person will have companions and contemporaries with whom he associates, and with whom he spends a lot of his time. Therefore, seize the opportunity in accompanying the best of the people in whose company there is not an absence of knowledge to be learnt, an advice to be benefitted from or pre-occupation with that which draws one closer to Allaah. The least (acquired in their company) is that you will be safeguarded from being held responsible in speech and action, alongside being safeguarded from being ridiculed, slandered and backbitten whether you are present or absent. There is also that great benefit of your heart’s desire for good being increased and cultivated, and the urge towards evil is either weakened or made to vanish.
Indeed, a man is upon the religion of his companion, so look to the one you take as a close friend. And regardless what you may receive of praise and respect from the people, they judge the people by their associates. Therefore, it is befitting that a person takes pride in accompanying the best of the people and beware of the evil ones. A righteous companion is like a seller of musk; he either gives you some or you get a good smell from him. And the evil companion is like the blower of the bellows, he either burns you or you receive a bad smell from him. And Allaah knows best.
[Source: Noorul Basaa-ir Wal-Albaab Fee Ahkaamil Ibaadaat Wal-Mu-aamalaat Wal-Huqooq Wal-Aadaab: page: 66. Abridged and slightly praphrased]