How do we know that we have exactly 7 sacraments?
We know because the Church tells us.
Sacraments are mysteries, from the Greek for something hidden. They reveal God to us individually, as Jesus revealed his Father to the world. This is more than just scripture, it is an unveiling of God in our lives.
I usually describe the sacraments in 3 groups of 3:
Initiation: Baptism, Confirmation, Eucharist.
Healing: Reconciliation, Anointing the sick, Eucharist.
Way of life: Matrimony, Orders, Eucharist.
Each of these has their own scripture passages, but they are also more than that. For instance, the anointing of the sick rests heavily on the letter of St James, which describes how to care for the sick. But it also rests on the Gospel, where Jesus not only healed but told his disciples they would do greater things and sent them out to heal he sick. (Mark even mentions anointing them)
Reconciliation is similar, in that the Gospels portray Jesus forgiving people. Though we do not have the detailed instruction that we had for the sick, the idea of forgiveness is so rooted in the story of Jesus, it would be hard to think of God without it.
The number seven to describe he sacraments was only reached about 1000 years ago. Reflection on how God is revealed to us spent a number of years arguing about that number. 2? 10? 3? There are reasons for choosing 7, but ultimately it came down to where the Church sees God’s mercy revealed.
In the 3x3 scheme I suggested above, you can see some of the Church’s reflection. The Eucharist, thanksgiving, is obviously most important, as it is the beginning, our healing, and our way of life. But each of the others has its own way of showing us God’s grace.