Like Chronos said, halftime is pretty tightly scripted. Visiting band first was the convention everywhere I played.
Presence of both bands must depend on where you are. I went to high school in Texas in the late 80’s, and I was in the marching band. We went to every game - home and away. Both bands performing their on-field shows at halftime was completely normal, to the point that I can’t recall any games being otherwise.
In the early 90’s I was in my college band; we performed at every home game and some road games. Out opponents seemed to behave similarly, so one- and two-band halftimes were both pretty common.
It was tight, but both shows could be squeezed in. In college, I believe a football team could incur a penalty if their band wasn’t off the field by the appointed time. Since the visiting band (if present) went first, it was always the home band up against the clock. And if the visitors overran their time, leaving the home band less time than planned - it was too bad for the home band. I believe we had to cut at least one show short because of this. The football game was definitely the priority.
As far as the bands coordinating playing “incidental music” from the stands during the game, my memory is a little more fuzzy. I’m not sure how it was handled in high school. In college I want to say that there was a convention for extended pauses like time-outs - home band gets odd-numbered quarters, or something along those lines. After a score, the scoring team’s band “had the floor” without exception.
In college, our conference had a rule that bands could not play during action on the field, including the huddle and pre-snap periods. So except for extended pauses like time-outs, we were restricted to really brief periods of playing. Our band had specially arranged a dozen or so “tags”, as we called them - 15-20 second snippets of oft-played tunes. These were called out by number and squeezed in between plays seemingly at the director’s whim during “our’” quarters (or the whole game, when we were the only band).
After our team scored a touchdown, we would immediately launch into our school’s fight song. The tune was long enough that it usually wasn’t over by the time the PAT attempt was lined up - on our director’s signal we’d get extremely quiet while continuing to play, rising back to normal volume as the kick sailed (hopefully) through the uprights. This was apparently sufficiently compliant with the “no playing during game action” rule.
In high school there was no such rule. Our practice was to generally only play the fight song at appropriate moments during the first half, basically keeping our eyes on halftime. Halftime was, from the band’s perspective, a dress rehearsal for the marching band competition performance. We’d normally take the 3rd quarter completely off, and then return for the 4th quarter where we’d explore our repertoire book a bit (always prepared to stop mid-piece to launch into the fight song when on-field events warranted). And like I mentioned earlier, I don’t recall how the bands avoided playing over each other. Possibly some hand-signal communication between directors that I was too oblivious to notice.
At all levels, tune selection in the stands was communicated from the director/drum major either by hand signal (e.g., a raised fist for the fight song) or simply shouting it out and letting it be passed back by word of mouth.
A good performance at the marching competition in late October was the object of everything we did all season - and if a band did well they could advance to subsequent rounds. Once we were done with competing, the atmosphere (for us) at football games lightened considerably. Most significantly, our marching rehearsal time dropped dramatically to “maintenance-level” and the time began being spent indoors preparing for the concert band season (which had its own competition in the spring).