There should be no gap. Looks like shoddy construction.
See figure 16 from this page for proper alignment:
Also I have never seen a "band" or "rim" joist that are configured in the fashion described by your image. It can't be good that half of the wall is resting out in space.
You could rip a board to fit the gap so that the weight of the wall is transferred to the foundation evenly, but I would probably call a professional to see if that option is even structurally sound. I would not fill it with spray foam. Fix the structure first, then insulate.