You can take a tapestry needle and weave the ends through the adjoining stitches.
If it is wool, the books say that you can split the wool, moisten it and rub it between your hands to felt it. I honestly don’t have a lot of luck with this, but I’m not all that patient. When I’m being a ‘good girl’ I trap the yarn before I change by twisting it for a few stitches before I change. If I forget, I often knot if it won’t make a difference.
I usually knot when I’m to lazy to do anything else, but I always weave my ends in as I knit, by trapping them under the next few stitches. This way, when I’m finished, I just have to cut off those long ends.