We're told again and again that the goal of the invasion is the legalized occupation of Naboo by the Trade Federation. The whole thing turns out to be a giant black eye for the bad guys though. By the end of the invasion, the evil Sith after a millenium in hiding return only to see Darth Maul cut in half by a Jedi padawan on his first mission out while the fearsome droid army of the Trade Federation are defeated by a planet with no military using two Jedi, a handful of volunteer security guards and some Gungan cannon-fodder.
When you're a Dark Lord of the Sith trying to conquer the galaxy, you can't have just one plan on how to do it. You need contingency plans for when things go off the rails and then contingencies for those contingencies. Captain Panaka was a deep cover asset who, though not a formal part of the plan to invade Naboo, tried his best to ensure the Trade Federation's invasion was a success by subtly working against Queen Amidala and ultimately ended up turning the entire fiasco into a victory for Palpatine.
Palpatine and Panaka are both from Naboo. In their respective positions as senator and head of security, they would at least be well acquainted with each other professionally. Panaka would also likely also know a great deal about Palpatine's private life and routine when he's on Naboo as part of his job.
As Palpatine's agent, Panaka knew about the impending invasion, but downplays the possibility of attack when the Trade Federation jams their communication. If Governor Baldie McMullet immediately understood that blocked communications were a precursor to invasion, so would a head of security.
Panaka tries to use fear to manipulate Amidala into surrender by telling her that there is no way Naboo can defend itself against the droid army.
When the Jedi rescue them and Qui-Gon asks her to go to Coruscant for her own safety, Panaka tries to keep her on Naboo by assuring them the Trade Federation would not harm Amidala because they needed her to sign the treaty.
After the ship is damaged, Qui-Gon decides to land on Tatooine because there is no Trade Federation presence there. Panaka objects.
Once they are on Tatooine, there is no way for Palpatine or the Trade Federation to know where they are, so someone would have to send out a signal to them with their location. There are three people on board that might suss out his intentions to send one: the two Jedi and the Queen. Luckily, Qui-Gon has to leave the ship to get parts while Obi-Wan is preoccupied with repairing it. So what does Panaka do? He pawns the Queen off on Qui-Gon so he'll take her with him into town. With her gone, he's free to signal Palpatine with their location.
When they finally get to Coruscant and met Chancellor Valorum, he makes a half-hearted, almost dimissive bow (really more of a head nod) to him in stark contrast to the full bow he gives Palpatine later in the movie after he becomes chancellor.
If there was one time when security should stay by their VIP's side at all times, it would be while on foreign soil. Yet on Coruscant, Panaka ditches Amidala to hang out with Palpatine to do who knows what, but when he returns, he is very excited to announce Palpatine was nominated to succeed Valorum.
When Amidala decides to return to Naboo, he tries to convince her to stay on Coruscant which is the exact same thing Palpatine now wants.
Arriving back on Naboo, he tells everyone that security forces and police have started an underground resistance. What does he do next? He announces that he has rounded up as many of its leaders as possible and brought them back with him to the plains which would leave said resistance unorganized and effectively without leadership during their push to take the palace. He then tries to dissuade Amidala from going through with the attack again.
He literally takes Amidala through a window straight into a trap in the throne room where they are immediately surrounded by droidekas. Had Amidala's decoy not shown up, the Trade Federation could have ended things right there.
As the movie ends, he leading Gunray away and unlike the weak bow he gave Valorum, he gives his true master Palpatine a deep formal bow.
TYING UP LOOSE ENDS
If Panaka was working for Palpatine, why doesn't he side with Gunray in the throne room once Amidala surrenders? Because he's an undercover agent and undercover agents stay in character until their handler pulls them or issues them new instructions regardless of what's going on at the time.
Panaka is part of Palpatine's spy network. He had two jobs. First, it was staging for the invasion. He made sure the droid army had absolutely no resistance in taking the capital. His second job was to keep tabs on the Queen. Palpatine being a politician from Naboo, would have known she used a double, so he needed someone to keep track of the real Queen during the invasion.
At the end of The Phantom Menace, once Nute Gunray has completely bungled the invasion and been apprehended, Panaka receives his new orders from Palpatine. Gunray is in Jedi custody on a planet that is 100% hostile to him. Yet, when Attack of the Clones picks up, he's not only free, but on the ruling council of the Separatists. How is that possible? Because Panaka is the one who led him away into detention and let him escape.
Through his hard work, Panaka ensures that a failed one-off deal between two politicians turns into a permanent alliance with the war criminal Nute Gunray and his Trade Federation now jointed at the hip with the Sith and dependent on Palpatine for survival. Palpatine uses the Republic to try Gunray four times in absentia to keep him in remembrance of that. For a job well done, Palpatine likely rewarded Panaka and brought him into the fold over in Coruscant which is why he doesn't return for the rest of the trilogy.