This is because in medieval Italian poetry, poems were often written to be sung or recited in different rooms of a castle or palace. Each room would contain a distinct section or division of the poem, which came to be called a "stanza".
Over time, the term "stanza" evolved to refer to any distinct section of a poem, regardless of the context in which it was originally written.