Even by the end of SEASON TWO, Star Trek was a ratings flop. And yet NBC was willing to roll the dice on a sequel?
It's not really a spin-off series, it was a "back door pilot".
Desilu was being sold to Paramount at the time, but pilots still have to be made, otherwise no new shows get greenlit. For every pilot that becomes a series, there are probably twenty or so that don't, and only a few of those can survive on their own and actually get airplay, thus recouping some of their expenditure through the TV advertising.
Consider: if a pilot was made for "Assignment: Earth" but it had no Star Trek elements were in it, what TV station would run a single episode of a show that never existed beyond one episode? The money is wasted, unless it could be used in some kind of anthology show, such as "The Twilight Zone". Remember "Love, American Style"? One of its segments was called "Love and the Happy Day", which became the TV series "Happy Days".
Or, the studio takes an even bigger gamble and makes
a whole telemovie out of it (eg. the original pilots for "The Six Million Dollar Man" and "New Original Wonderwoman"), so it can at least be aired as a movie-of-the-week.
Someone had come up with a concept of a human, secretly trained as an agent by aliens, working with a 60s ditzy girl Friday, going on missions together to keep the timeline intact. That would have been expensive to commission as a pilot, but
as an actual episode of Star Trek, the budget for the pilot would be wholly covered by the Star Trek budget. Executives being show the episode, perhaps recut with a new opening, can easily imagine Kirk and Spock being replaced by other characters each week to interact with Gary Seven and Roberta.
Sometimes a spin-off show is developed because certain characters seem to have potential as the star of their own vehicle, eg. "Rhoda" spun from "The Mary Tyler Moore Show", but she was already very much an integral part of the original show. I'm not sure if Mork from Ork ("Mork and Mindy") was considered to be a potential spin-off from "Happy Days", or not, before the episode was aired. It may have been that Robin Williams happened to make a good impression, and a new series concept was born as a result of the feedback from audiences.
But sometimes a concept for a new show gets shoehorned into an existing show, to prove that it might have legs, and to have the pilot made, essentially for no additional costs.