Thr proximity sensor should turn off the screen before it touches your face.
if it does turn the screen off near your face but not quick enough, try taking more time over moving it to your face. Touch your ear first by angling the top towards your face.
If the screen does not turn off at all it’s possible the proximity sensor is not kicking in.
It may well need looking at in an Apple store. You will have to back it up if it goes into Apple repair shop so why not try a back up and restore in iTunes first and see if it fixes it.
Restoring via iTunes will repair the iOS software on your iPhone.
How to back up your iPhone, iPad, and iPod touch - Apple Support
After backing up, restore via iTunes to repair the iOS software. You should test by setting it up as “new” after restoring:
Restore your iPhone, iPad, or iPod to factory settings - Apple Support
If it seems to work OK after setting up as a new iPhone, restore your backup and test again:
Restore your iPhone, iPad, or iPod touch from a backup - Apple Support