It’s trivial to write any positive integer as a sum of powers of $3$ if you allow repetitions; you really meant to ask about writing positive integers as sums of distinct powers of $3$. It is not possible to write every positive integer in that way: you clearly cannot write $2$ as the sum of distinct powers of $3$.
In order to use the weights $1,3,9$, and $27$ kg to weigh every whole number amount from $1$ through $40$ kg, you must be using a two-pan balance, and you must allow the weights to be placed in either pan. The scales balance when the weight of the object plus the weights in its pan equal the total of the weights in the other pan. Thus, to weigh a $5$ kg object, for instance, you must put the $1$ and $3$ kg weights in the same pan as the object and the $9$ kg weight in the other pan. The scales then balance, because $5+1+3=9$. Equivalently, $$5=9-3-1=3^2-3^1-3^0\;.$$ Thus, it’s not a matter of writing $5$ as a sum of distinct powers of $3$: we write it as a sum or difference of distinct powers of $3$.
This is indeed always possible. (Indeed, it is possible to write every integer as a sum or difference of distinct powers of $3$.) For example, $19=3^3-3^2+3^0$. We could abbreviate this to
+-0+, where the plus sign indicates that we add the power, the zero indicates that we don’t use it at all, and the minus sign indicates that we subtract it. The resulting positional notation for integers is called balanced ternary notation, and you can find quite a lot on it on the web. This PDF gives a reasonable introduction.