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.
It is true that Saiyans would not give up. But Goku had lots of characteristics which doesn't go with saiyan race.
The reason for this will be obvious if you carefully look at the childhood history of Goku. Even as a child Goku was very much a saiyan when Grandpa Gohan found him. He was very aggressive and short tempered and uncooperative. But after an accident where Goku falls from a ravine and gets hit on his head, he changes completely. He became a cheerful, loving regular boy. This incident attributes to many of the Goku's personality traits which are uncharacteristics of a saiyan, like sparing the defeated enemies, giving up when situation demands etc.,
Goku had a fair idea about Cell's 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 it's 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 you 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 had hidden powers sleeping, coming out in a training session. And Goku understood, if anybody had a chance against Cell, it was his son!
Now, coming to your question. 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 Cell's technique up close and personal watching his dads battle with him.
Goku knew all along he won't win, so when he thought his purpose of showing Gohan a good glimpse of Cell's fighting style, it was time to call the battle off and make way for his son to finish things off.
So that's 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.
When Goku was fighting Cell, he wasn't trying to win the fight. He wasn't even trying to tire out Cell to make the fight easier for Gohan. He was just trying to draw out all of his techniques so everyone watching could understand the opponent that was otherwise so mysterious. This was his plan from the very beginning of their training, but unlike Picollo he didn't pressure Gohan with this, he kept it a secret.
Goku knew that he couldn't win the fight against Cell, but he didn't give up because he was afraid to die, he gave up because he wanted to send a message to all of his friends who relied on him that he won't always be able to protect them, and that they have the power to protect themselves as well. Goku does a great job explaining this mentality at the very end of Dragonball Z when he says goodbye to his friends to train Uub. Goku knows that if the whole world relies on him everytime, then it will never truly be at peace. the people need a way to protect themselves, and the fight against Cell was the first time he really went forward to push this idea.
It is incredibly unlikely for a Saiyan of any temperment to give up in a tough fight, ESPECIALLY against a very strong opponent. This was no doubt really difficult for Goku to do, but he knew if he had just died, then gohan would never have had the confidence, or the composure to fight at his true potential. He needed his dad to tell him he can do it.
So, Why does Goku give up against Cell?
Because he needed to show Gohan that he had surpassed even his own father in strength and speed.
The main purpose of Goku entering the Room of Spirit and Time was to train Gohan. Goku was cognizant of Gohan's abilities - Gohan's ability to take his current level to another level when provoked. Gohan did this against Raditz, and Frieza (remember who when crazy on Frieza's 3rd form? Not Piccolo). Goku's idea what that if Gohan got to Goku's level, then that classic +1 ability would push Gohan past Cell. The purpose of Goku's fight was to show Gohan Cell's fighting style. That's all. Goku was confident. They had him (Goku), Gohan would could likely go into a higer berzerk level and a whole bunch of Senzu. Incidentally, Dragonball Z was a transition of the show being about Goku, to it being about Gohan. (dragonball was about Goku)
That is partly true. Goku was stronger than Cell but he wanted Gohan to defeat him. Simple as that. Later in otherworl Pikkon defeated Super Perfect Cell like he was nothing, and Goku and Pikkon were evenly matched when they were fighting in the otherworld tournament. So from that, Goku was able to defeat Cell.
Even if Pikkon was slightly stronger than Goku, Cell was still no match for him.