This is a matter of what linguists call do-support.
The inversion which we employ in yes/no questions like "Did you see him?" and Were you with him?† is subject-auxiliary inversion—that is, the subject inverts only with auxiliary ('helping') verbs: have, be, do and the modals can/could, may/might, must, shall/should and will/would.
If the declarative version of your sentence has an auxiliary, you invert it with the subject to make a question:
SUBJYou AUXhave seen him → AUXHave SUBJyou seen him?
But lexical ('main') verbs, like see, which can stand alone without an auxiliary, don't invert;‡ so if the declarative version uses one of these verbs without an auxiliary, how do you make a question? —You bring do in to act as the auxiliary you need, and invert that with the subject:
SUBJYou saw him → SUBJYou AUXdid see him → AUXDid SUBJyou see him?
There's just one tricky thing here. Have, do and be can all act as either auxiliaries OR lexical verbs. When you use do by itself as a lexical verb, as in do your homework, it requires do-support for inversion in questions:
Did you do your homework?
But be always acts as an auxiliary and inverts without do-support, even when it's the only verb in the clause:
SUBJYou AUXwere with him → AUXWere SUBJyou with him?
And lexical have is betwixt-and-between: in ordinary speech today it requires do-support in questions:
Do you have any questions?
But for a long time lexical have could be used like an auxiliary, and there are still a few people who use it that way:
Have you any questions?
† and in negations like I don't see him—but we won't worry about that here.
‡ Once upon a time they did invert—Saw you the field? Came you from Shrewsbury?—but that use mostly disappeared some 400 years ago, with a couple of exceptions addressed later.