When I started with my teacher many moons ago, teaching proper pedal and understanding pedal was one of his priorities. That is different than just rules about what to lift or raise when. Listening and what to listen for was part of it. I'll try to give a comprehensive summary - including the obvious. π
mechanically: The piano strings vibrate when struck by the hammer, and stop vibrating when the dampers fall on them. If you pluck a guitar string and then put your palm in the string, you stop the vibration. When you press a key, the damper over the respective string is lifted, and then the hammer sails over striking the string making it ring. When you release the key, the sound stops. UNLESS something else holds the damper off the string(s) - that being the right damper pedal. Therefore your hand and your foot take turns lifting the damper off the string(s).
sound: For a legato sound, you want one note to almost blend into the next note so that there is no gap in the sound. Supposing you're playing a C chord, then Dm, then Em. The C chord is ringing as you move your hand over to the Dm chord because of the pedal. At the moment that you strike Dm, you don't want C to blend in with Dm, so that's the moment you "erase" the C by lifting the pedal. You immediately lower the pedal to catch your new Dm chord. Once the pedal is holding up the dampers - allowing Dm to continue ringing - you can release your hand from the cord / finger from the note.
Use your ears to guide you. When you can lift your hand is determined by when the new notes are "caught by the pedal" which actually means, when the pedal is holding up the dampers. Also use your ears to guide you about when to lift the pedal.
Experiment with joining two notes or two chords using the pedal, with trade-off of hand and foot, to discover different effects, and to start hearing how an old sound sustains or not. When I began, I was shocked by what I was not yet hearing. I'm still learning to "hear".