When to use "onboard the ship" and "on board the ship"?
"Onboard", one word, is wrong.
"On-board" (hyphenated) is an adjective.
"The captain is on-board."
The on-board meals were delicious."
"On board" (not hyphenated) + noun is a prepositional modifier phrase as well as having other functions:
"We ate our dinner on board the yacht."
In both of your examples, the word "aboard" would have been much better.
OED
a. on board [...] has now, in common use, the meaning: On or in a ship, boat, etc.; into or on to a ship.
[The] fuller form [was] on ship-board (cf. Middle English ‘within schippe burdez’ [where "burdez" = sides]), and the construction ‘on board of the ship’, or ‘on board the ship’ (where ... ‘board’ means 'the deck').
[...]On board appears to be a later expansion (...) of aboard adv. and preposition, and this to have been taken directly from French à bord, ...in which bord = ‘ship's side’ comes contextually to be equal to ‘ship’ itself. ... On board, first appears late in the 17th cent.
Navy: Use aboard when referencing events taking place on a ship or aircraft. Use onboard when discussing shore based events.