The apples are bad
They are lying on the table.
The apples that are lying on the table are bad.
that in the third sentence has the same grammatical function as they in the second - subject of the verb are lying.
The apples are bad.
We bought them in the shop.
The apples that we bought in the shop are bad.
that in the sixth sentence has the same grammatical function as them in the fifth sentence - object of the verb bought.
Student or Learner