How far does a cooktop have to be from a wall?

There is no requirement for spacing a cooktop from a wall.

Practically speaking, you usually end up between 1.5″-3″ from the wall in most 24″ deep counters.

Most installers will either center the depth or place the front edge within 3″.