I think the key to remain in good terms is to say “no” as soon as you know it. If you make them lose time unnecessary, and that could mean losing candidates, no matter how nice you say it you’ve caused them problems and they won’t remember you in a good light.
I have been in a couple of scenarios.
One is when I’ve received and accepted a job offer in the middle of a job search where I had other interviews still lined up. In that case as soon as I accepted the offer I reached out to the other interviewers to let them know I had just accepted a position, ask to withdraw from their selection process, and wish them the very best.
I’ve been to places where during the final on-site interview I realized I wouldn’t want to work there. That happened twice to me, and in one of the places at the end of the day the hiring manager had told me that if I wanted the position it was mine. I take that as an “unofficial offer”. In both cases I immediately contacted them (like in next morning) to say no, and the way I did it was to tell them that I did not think it was the right position for me at the time. I thanked them for the time, praised the team and the work they are doing, and wished them the best in their selection process.
I think that questioning the fit with the position (not with the team/company) and framing it into “at this time” (not forever) leaves many doors open for future interviews for other positions. And tell them as soon as you know you will not be accepting that position!