During the Cell games, Goku knew he wouldn’t be able to defeat Cell. But it’s unlike a Saiyan to ever give up. Goku didn’t give up when he fought Vegeta, or Freiza, or anyone else for that matter. But why does he give up against Cell? This isn’t consistent with Goku’s character.
Goku had a fair idea about Cells level after he attained his perfect form. Goku was looking for answers for how can he surpass the level of a super saiyan, or if its really possible. Luckily they had something called the time chamber to their disposal which gave Goku plenty of time to figure it out.
Goku managed to come up with an ingenious idea to increase his stamina and increase his power whenever required by using up his reserves of energy which he could increase by improving his stamina. He understood the flaw in Vegeta and Trunks’ techniques.
Now, if we recall; Goku didn’t use the time chamber twice trying to push his new found powers further, which BTW was very unlike Goku who uses every opportunity when he faces such crises (remember before facing Frieza how he goes all out till 100G in his training). The reason for that was, Goku already found that Gohan; who had hidden powers sleeping coming out in a training session. And Goku understood, if anybody had a chance against Cell, it was his son!
Goku didn’t need to fight in the first place, because he knew Cell will beat him to the ground when the battle is over. But he still fights for 2 reasons:
As a Saiyan warrior, challenges in combat like these are what they live for.
He wanted to show Gohan Cells technique up close and personal watching his dads battle with him.
So thats the reason behind Goku’s giving up, which Goku will die as a warrior before doing. But it was a teaching in disguise for his son.