The reason for this mitzvah is pretty apparent: if you take something that doesn’t belong to you, give it back!
Now, for some details. The Torah says “which he stole.” The Talmud (Baba Kama 112a) infers from these words that the object must be returned in the same condition. If he stole potatoes, he must return them. If he already made them into French fries, he doesn’t return the fries, he pays for the potatoes. This is true for any substantive or irreversible change. If a thief stole raw wool and made a sweater or stole logs and built a shed, he would pay for what he stole. But if he stole spun wool or lumber and the sweater or shed could be taken apart to restore the object as it was when it was stole, he returns the object itself (see Baba Kama 93b).
The Talmud discusses to what lengths one must go to return the actual object. For example, if the rightful owner is overseas, does the thief have to deliver it in person or can he entrust it to the local courts (Baba Kama 103a)? If the thief stole a beam and built it into a house, must the entire building be demolished to return the beam (Gittin 55b)? Many more such scenarios are discussed.
So as not to leave you hanging, the answers to the questions posed in our examples are: (a) the thief can leave the object with Beis Din for safekeeping and (b) he can repay the value of the beam. The reason for this is, even though he should be required to travel or to demolish the house, the Sages made these exceptions because if it would be too onerous, people would be discouraged from coming forth to make good on these debts.
This mitzvah applies to both men and women in all times and in all places. In the Talmud, it is the topic of the ninth and tenth chapters of tractate Baba Kama. It is codified in the Shulchan Aruch starting in Choshen Mishpat 360. It is #194 of the 248 positive mitzvos in the Rambam’s Sefer HaMitzvos and #68 of the 77 positive mitzvos that can be fulfilled today as listed in the Chofetz Chaim’s Sefer HaMitzvos HaKatzar.