That's a common problem. Windows sound system is so complex that a full route through it can take an intolerable long time.
Common solutions to reduce the latency:
Use an external mixer which outputs to your phones the piano sound directly from the piano, mixes to it the possible reference output from the computer or another music player and directs the piano sound also to the computer for recording.
Use a properly designed audio workstation software for recording. Such programs generally allow low latency ASIO audio drivers. Utility program ASIO4ALL (freeware) uses normal computer audio hardware, but bypasses the normal long latency Windows sound system drivers and makes the audio workstation program able to see an ASIO4ALL as a selectable audio device.
Get ASIO4ALL from here https://www.asio4all.org
The latency will reduce to a few 10 milliseconds. It's not perfect, but it's something that you maybe can accept.
- Use an external USB audio interface for recording instead of computer's own audio hardware. Such interfaces give often the direct monitoring option. It works as well as using an external audio mixer. They have also low latency ASIO drivers. Cheapest "plug and play, no installations needed" -type devices do not have them. Check the specs and read reviews, find local help if you do not understand the terminology.
As said, high quality external audio interfaces generally have low latency ASIO drivers, but ASIO4ALL is a workaround for those who want to use computer's own audio hardware and cannot use a hardware mixer for low latency monitoring.
Warning: Audio hardware with ASIO audio driver cannot be used at the same time by several applications. The low latency is achieved by making the internal function straightforward.