I came across a sentence that read 'It was the first to be sold online'. It used the passive voice. Why not 'first sold online'? Why is to be used here and when do we use to be in the passive? Thanks
"Sold online" without the verb "to be" (in its appropriate form) is not the passive voice. It is a past participle, which, on its own, functions as an adjective.
The verb "was" in the beginning of the sentence is not tied to "sold online", so it is not part of the passive.
Compare the following sentences,
— He was the first to notice that something was wrong.
— From all the children in the family, April was the first one to open her Christmas presents.
As you see, here we are simply talking about someone being "the first (one)" to do something.
So we are dealing with the following construction,
to be the first (one) + Infinitive
In your sentence, something was the first..... to be sold (which is the Infinitive form of a passive construction).
The first "was" has nothing to do with "sold", it is tied to "the first" (to be the first). So, in order for there to be a passive after that, that passive must contain the verb "to be" in some form.