The instructions Paul gave Timothy in 1 Tim. c 5 was dealing with the members of the assembly, and specifically in vs. 17 -19 of treatment of the elders. Because of this most commentators attribute the "laying on of hands" in vs. 22 to only be speaking of the appointment of elders (ordination).
But, the laying on of hands was not just for appointment of elders (Acts 6:6), or missionaries or special office (Acts 13:3), but also of imparting the special gifts of the Holy Spirit which was poured out on that generation in the first century A.D. (Acts 2:39).
We have an example in Acts 8:14-24 of Simon the sorcerer who wanted the power of the Holy Spirit for monetary gain. So, the laying on of hands was not indiscriminately applied, nor the Spirit indiscriminately given.
The gift of the Holy Spirit was not an automatic consequence of baptism as was demonstrated in Act 19: 1-5. It only came by the laying on of the hands of the apostles, or one whom they had appointed such as Timothy.
We know from Acts 6:6, and 13:3 and other places that the apostles prayed before they laid hands on anyone, which was asking our heavenly Father whether or not those baptized (immersed) should receive the gift. So, the Spirit always went where He wills (John 3:8), and was not poured out on everyone.
But Paul cautioned Timothy to consider carefully, to not be hasty in laying on his hands to anyone - whether for the gifts or for office of service. Because without careful consideration and prayer Timothy would be a partner to whatever sins the other was engaged in.
Excerpt from Meyer's NT Commentary on 1 Tim. 5:22 -
"Hofmann thinks that it is used of the appointment to a church office; but of this there is no hint in the context. It will be most correct to take the exhortation quite generally, so that the meaning is, Timothy is to lay hands ΤΑΧΈΩς, i.e. “in over-hasty fashion,” on no one—whatever the occasion may be. The reason why not, is given in the next words: μηδὲ κοινώνει ἁμαρτίαις ἀλλοτρίαις. The ἈΛΛΟΤΡΊΑΙ ἉΜΑΡΤΊΑΙ are not, as Hofmann thinks, the sins of those who are hasty in the laying on of hands, but the sins of those on whom hands are too hastily laid. He who thoughtlessly lays hands on the unworthy, thereby declaring them worthy of the divine blessing, makes himself a sharer in their sins. Against this Timothy is to guard; he is rather to observe what Paul expresses by saying: ΣΕΑΥΤῸΝ ἉΓΝῸΝ ΤΉΡΕΙ. This exhortation is in itself quite general, but it stands here in close relation to the foregoing warning. Timothy is to keep himself pure (ἉΓΝΌς as in 1 Timothy 4:12, not in the special meaning “chaste”), particularly in not making himself a partaker of others’ sins by laying hands on them too hastily. " Source: here
And, from Vincent's word studies:
"Letter, make common cause with. See on communicating, Romans 12:13. Comp. Romans 15:27; 1 Peter 4:13; Ephesians 5:11. By a too hasty and inconsiderate restoration, he would condone the sins of the offenders, and would thus make common cause with them." Same source as above.
Paul's exhortation may stem from Lev. 19:17 where the instruction was to rebuke your neighbor when you see him sinning, because silence is agreement, and thus becomes acquiescence and participation in the same sin. See also 2 John 1:9-11.