Best way to seal gaps in sliding window?
Spray foam would be excellent. Until it's time to take the AC out of the window and you're left with a disastrous mess of sticky foam that you can't scrape off to get the window to seal again.
Get some pieces of foam rubber and cut them to fill in the gaps between the sashes where they don't seal because the window's open. You could use pipe insulation, pool noodles, or packing foam that came with some product you ordered in the past.
You may not have a perfect seal and may have to deal with a few insects that get through, but that's better than a hot, humid room. To be fair, though, I used to do this when we put a window AC unit in an upstairs window, but were able to seal it up well enough (with old, rickety, leaky wooden single-pane windows) that insects weren't a problem. Considering that we live in the Midwestern US where summer bugs can be a considerable issue, it worked pretty darn well for us.