Copyright law allows some exceptions. In the U.S. one of the main exceptions to copyright is Fair Use Doctrine.
Educational purposes like scholarship, research, and analysis often qualify as fair use, as portions of the copyrighted work are reworked and transformed from the original through analysis and commentary. Other "transformative" uses add substantive new expression or meaning to the original work.
A stronger case can be made for Fair Use with a published work than an unpublished work as creators have the right to control the public appearance of their creations. Use of imaginative works (a song, poem, photograph, or play) are less likely to be considered fair than factual works such as biographies or news stories.
A smaller portion of a copyrighted work is more likely to be considered fair, for example a tenth of the work or a chapter in a longer book. In addition to amount, consider the degree to which the portion taken captures the most memorable or substantial "heart" of the work. For example, the Stanford Copyright and Fair Use Center notes "it would probably not be a fair use to copy the opening guitar riff and the words 'I can't get no satisfaction' from the song 'Satisfaction.'"
Use should weigh the impact on the copyright owner's income (or potential future income) from their work.
Stim, R. Measuring fair use: The four factors. Copyright and Fair Use: Stanford Libraries. https://fairuse.stanford.edu/overview/fair-use/four-factors/
U.S. Copyright Office. (2021). More on fair use. U.S. Copyright Fair Use Index. https://www.copyright.gov/fair-use/more-info.html