It is possible to stay inside the park. There are some nice lodges, the Ahwahnee, the Yosemite Lodge, and Curry Village among others, and campgrounds, a cheaper alternative. It is always necessary to book ahead. If you come in Summer you may have to book several months in advance.
A good alternative is sleeping outside the park. Groveland, in the picture, is a good option.