Aokiji is a former Navy Admiral, who is currently a member of the Blackbeard Pirates. It came as a big shock to me and probably you when they found out that Aokiji had left the Marines and joined hands with Blackbeard. This covers the reason why Aokiji left the Marines and teamed up with Blackbeard.
He chose not to arrest or kill Luffy because the young pirate had defeated Crocodile beforehand, thus saving Arabasta from the latter’s malevolence. The thing called “justice” changes its shape… Depending on where you stand. — Kuzan’s view of justice.
After Sengoku retired from his Position as fleet Admiral, either Akainu or Aokiji could’ve become the new fleet Admiral, so they had a fight for the Position on Punk Hazard. Aokiji lost this fight, so he left the Marine as he didn’t want to Serve under Akainu.