We use 'only' when we want to exclude other things or possibilities.
There was only one piece left.
He was an only child
'Just' can be used in most of the situations above, but it is also used for other meanings, meanings for which 'only' would not be used, or would have to be used within a phrase.
He just missed the mailbox - He only missed the mailbox by a little.
It is just 12 centimeters = It is exactly 12 centimeters.
He just got here = He got here only a moment ago.