user23658: I moved my king as I wanted it to be in a safer location and after I did my turn my opponent realised that I put my king in check by accident and as the rules state you cannot put your king in check we did not know whether we should go back to my last turn or if my opponent had won the game.

user12873: In this case you would just go back to the last turn. In official tournaments, a rule used is you get a penalty on the first time you make an illegal move, and then if you make 1-2 more illegal moves (for FIDE it's 1 more) you lose the game. But assuming this game was just for fun, as it seems no arbiter was involved to help out, just take the move back and continue the game.

user4418: You noticed immediately, which makes it very easy because in this case the rules are the same (apart from time penalty) for all forms of chess. In the first instance you have to take back the illegal move and make a legal move. If possible you have to move the piece you touched. So, if your king could legally move to another square that's the move you would have to make. If it could legally move to several squares then you could choose between them.
For a first offence your opponent gains time on the clock. At blitz this is 1 minute and for standard and rapid it is 2 minutes. A second offence loses the game. But note that these penalties only apply if you pressed the clock before your opponent noticed and told you. If you did not press the clock then there is no penalty. You just have to make a legal move with the piece you touched.
The rules governing this are the FIDE Laws of Chess. This is what they have to say:

7.5.1  An illegal move is completed once the player has pressed his clock. If during a game it is found that an illegal move has been
  completed, the position immediately before the irregularity shall be
  reinstated. If the position immediately before the irregularity cannot
  be determined, the game shall continue from the last identifiable
  position prior to the irregularity. Articles 4.3 and 4.7 apply to the
  move replacing the illegal move. The game shall then continue from
  this reinstated position.
7.5.5   After the action taken under Article 7.5.1, 7.5.2, 7.5.3 or 7.5.4 for the first completed illegal move by a player, the arbiter shall give two minutes extra time to his opponent; for the second
  completed illegal move by the same player the arbiter shall declare
  the game lost by this player. However, the game is drawn if the
  position is such that the opponent cannot checkmate the player’s king
  by any possible series of legal moves.

Those are the rules for standard time control where the moves are being written. Because the moves are being written down it doesn't matter if the illegal move is noticed after several more moves were played, you still have to go back to the position when the illegal move was made and make a legal move instead.
For rapid and blitz this usually isn't possible and so the rules say that the illegal move has to be flagged before the next move. Appendix A in the Laws deals with rapid and the rule says:

A.4.2 If the arbiter observes an action taken under Article 7.5.1,
  7.5.2, 7.5.3 or 7.5.4, he shall act according to Article 7.5.5, provided the opponent has not made his next move. If the arbiter does
  not intervene, the opponent is entitled to claim, provided the
  opponent has not made his next move. If the opponent does not claim
  and the arbiter does not intervene, the illegal move shall stand and
  the game shall continue. Once the opponent has made his next move, an
  illegal move cannot be corrected unless this is agreed by the players
  without intervention of the arbiter.

Appendix B deals with blitz and that says:

B.4 Otherwise, play shall be governed by the Rapid chess Laws as in
  Article A.2 and A.4.

So, the rules are the same as for rapid, but for the penalties:

B.2 The penalties mentioned in Articles 7 and 9 of the Competition
  Rules shall be one minute instead of two minutes.


23658: <p>I moved my king as I wanted it to be in a safer location and after I did my turn my opponent realised that I put my king in check by accident and as the rules state you cannot put your king in check we did not know whether we should go back to my last turn or if my opponent had won the game.</p>
 12873: <p>In this case you would just go back to the last turn. In official tournaments, a rule used is you get a penalty on the first time you make an illegal move, and then if you make 1-2 more illegal moves (for FIDE it's 1 more) you lose the game. But assuming this game was just for fun, as it seems no arbiter was involved to help out, just take the move back and continue the game.</p>
 4418: <p>You noticed immediately, which makes it very easy because in this case the rules are the same (apart from time penalty) for all forms of chess. In the first instance you have to take back the illegal move and make a legal move. If possible you have to move the piece you touched. So, if your king could legally move to another square that's the move you would have to make. If it could legally move to several squares then you could choose between them.</p>

<p>For a first offence your opponent gains time on the clock. At blitz this is 1 minute and for standard and rapid it is 2 minutes. A second offence loses the game. But note that these penalties only apply if you pressed the clock before your opponent noticed and told you. If you did not press the clock then there is no penalty. You just have to make a legal move with the piece you touched.</p>

<p>The rules governing this are the <a href="https://handbook.fide.com/chapter/E012018" rel="nofollow noreferrer">FIDE Laws of Chess</a>. This is what they have to say:</p>

<blockquote>
  <p>7.5.1  An illegal move is completed once the player has pressed his clock. If during a game it is found that an illegal move has been
  completed, the position immediately before the irregularity shall be
  reinstated. If the position immediately before the irregularity cannot
  be determined, the game shall continue from the last identifiable
  position prior to the irregularity. Articles 4.3 and 4.7 apply to the
  move replacing the illegal move. The game shall then continue from
  this reinstated position.</p>
  
  <p>7.5.5   After the action taken under Article 7.5.1, 7.5.2, 7.5.3 or 7.5.4 for the first completed illegal move by a player, the arbiter shall give two minutes extra time to his opponent; for the second
  completed illegal move by the same player the arbiter shall declare
  the game lost by this player. However, the game is drawn if the
  position is such that the opponent cannot checkmate the player’s king
  by any possible series of legal moves.</p>
</blockquote>

<p>Those are the rules for standard time control where the moves are being written. Because the moves are being written down it doesn't matter if the illegal move is noticed after several more moves were played, you still have to go back to the position when the illegal move was made and make a legal move instead.</p>

<p>For rapid and blitz this usually isn't possible and so the rules say that the illegal move has to be flagged before the next move. Appendix A in the Laws deals with rapid and the rule says:</p>

<blockquote>
  <p>A.4.2 If the arbiter observes an action taken under Article 7.5.1,
  7.5.2, 7.5.3 or 7.5.4, he shall act according to Article 7.5.5, provided the opponent has not made his next move. If the arbiter does
  not intervene, the opponent is entitled to claim, provided the
  opponent has not made his next move. If the opponent does not claim
  and the arbiter does not intervene, the illegal move shall stand and
  the game shall continue. Once the opponent has made his next move, an
  illegal move cannot be corrected unless this is agreed by the players
  without intervention of the arbiter.</p>
</blockquote>

<p>Appendix B deals with blitz and that says:</p>

<blockquote>
  <p>B.4 Otherwise, play shall be governed by the Rapid chess Laws as in
  Article A.2 and A.4.</p>
</blockquote>

<p>So, the rules are the same as for rapid, but for the penalties:</p>

<blockquote>
  <p>B.2 The penalties mentioned in Articles 7 and 9 of the Competition
  Rules shall be one minute instead of two minutes.</p>
</blockquote>
