In a piano the sound is created by a the stings vibrating in at particular frequency which then in turn vibrate the molecules of the air and produce the sound while in a guitar the vibration from the string is passed into the hollow body of the guitar which then transfers these waves into the surroundings and ​vibrate the molecules of the air and produce the sound. Since the mechanism of production of sound in the two instruments is different that is the reason why the two instruments sound different even when the same notes are played with them.