Kakashi refuses to consider Gai his friend or lover or family because Kakashi doesn't think he can ever have and keep those things as his previous friends and family have all died. Kakashi literally told Sasuke he had no friends or family to be killed, and he really thought he didn't, even with Gai in his life. If Kakashi lets himself have those sort of connections again, he's convinced they'll just die too, so he rival-zoned Gai. Maybe he thinks that it'll be NBD for him if his rival Gai dies, maybe he genuinely believes that being close to Kakashi is what gets the people close to him killed.
And Gai? He's a doof, but he's emotionally intelligent enough to know this. He knows he's the closest person in Kakashi's life, and Kakashi keeps emotional distance from even him. So Gai plays up the rivalry, he's using any and everything that could be construed into a competition as a way to get Kakashi to engage with him. Gai doesn't want Kakashi to be alone and so if that means Gai has to be his rival then so be it. He'll happily play that role until he or Kakashi dies because he loves Kakashi and doesn't want him to be alone.