This is accomplished by what's known as binding off, a simple and quick method for making a finished edge.
When you complete the last row of your pattern (or have knitted until you are almost out of yarn, if you're not using a pattern with a definite end) you will begin to cast off just as if you were continuing the pattern.
You'll often see patterns that say "cast off in pattern," meaning that you knit the knits and purl the purls just like you were doing in the body of the project.
Start by knitting or purling the first two stitches of the row.
Once you have two stitches on the right-hand needle, use the tip of the left-hand needle to pull the first stitch back up and over the second stitch and over the tip of the right-hand needle.
This will leave one stitch, actually the second stitch you worked, on the right-hand needle.
Knit or purl the next stitch and repeat the process until you are left with no stitches on the left-hand needle and one stitch on the right-hand needle.
Slip this last stitch off the needle. Trim your working yarn so that you have a couple of inches left over to weave into the finished work.
Slip this yarn through the loop and pull tight, securing the yarn so the loop won't unravel.
Weave the end of the yarn through several stitches to secure the end, using either a sewing needle or a crochet hook.
Wear or use your new knitted item proudly!